比较joi中的两个对象

发布于 2025-01-24 13:37:15 字数 1410 浏览 0 评论 0原文

有没有办法验证一个对象中存在的密钥与另一个对象中存在的密钥相同?

例如:

{
  "localizationName": "en-US",
  "dataText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient info gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient info patient"
      }
  ],
  "purposeText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient data <<purspose>> gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient data <<purspose>> patient"
      }
  ]
}

按照上述示例,“ datatext”&amp; amp; amp; amp; amp; “ peleseText”。

我想要“ datatext中存在的任何角色”的JOI验证代码,也应在“ peleSetext”中定义相同的角色。

以下是我可以提出的JOI验证代码。但这似乎不起作用。

    localizationName: Joi.string()
        .valid(...BCP47LanguageTags)
        .required()
        .messages({ 'any.only': '"localizationName" must be a BCP47 compliant language tag like "en-US"' }),
    dataText: Joi.array().items(
        Joi.object().keys({
            persona: Joi.string().valid(...Persona).required(),
            verbiage: Joi.string()
        })
    ).required(),
    purposeText: Joi.array().items(
        Joi.object().keys({
            persona: Joi.string().valid(Joi.in('dataText')).required(),
            verbiage: Joi.string()
        })
    ).required()

任何帮助将不胜感激。

Is there a way to verify that the key that exists in one object is the same as the key that exists in the other object?

For eg:

{
  "localizationName": "en-US",
  "dataText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient info gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient info patient"
      }
  ],
  "purposeText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient data <<purspose>> gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient data <<purspose>> patient"
      }
  ]
}

As per the above example, the same personas exist in both "dataText" & "purposeText".

I want a joi validation code for 'whatever personas exist in the "dataText", the same personas should be defined in the "purposeText" as well'.

Below is the joi validation code I could come up with. But that doesn't seem to work.

    localizationName: Joi.string()
        .valid(...BCP47LanguageTags)
        .required()
        .messages({ 'any.only': '"localizationName" must be a BCP47 compliant language tag like "en-US"' }),
    dataText: Joi.array().items(
        Joi.object().keys({
            persona: Joi.string().valid(...Persona).required(),
            verbiage: Joi.string()
        })
    ).required(),
    purposeText: Joi.array().items(
        Joi.object().keys({
            persona: Joi.string().valid(Joi.in('dataText')).required(),
            verbiage: Joi.string()
        })
    ).required()

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半夏半凉 2025-01-31 13:37:15

我创建了一个函数,可用于比较所选对象指定键内的属性。我还假设您要比较的属性将在2个键中以相同的顺序为单位。

请参阅下面的代码:

var obj = {
  "localizationName": "en-US",
  "dataText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient info gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient info patient"
      },
      {
          "persona": "test1",
          "verbiage": "added this test"
      }
  ],
  "purposeText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient data <<purspose>> gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient data <<purspose>> patient"
      },
      {
          "persona": "test2",
          "verbiage": "added this test"
      }
  ]
};

function CheckPropertiesMatch(obj, keys, property){
  for (var i = 0; i < obj[keys[0]].length; i++){
    if (obj[keys[0]][i][property] == obj[keys[1]][i][property]){
        console.log("Properties at index " + i + " match!");
        } else {
        console.log("Mismatch at index " + i + "!")
    }
  }
}

CheckPropertiesMatch(obj, ['dataText',"purposeText"], "persona")

I've created a function that can be used to compare properties within specified keys of a chosen object. I have also made assumptions that the properties you want to compare will be in the same order within the 2 keys.

See my code below:

var obj = {
  "localizationName": "en-US",
  "dataText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient info gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient info patient"
      },
      {
          "persona": "test1",
          "verbiage": "added this test"
      }
  ],
  "purposeText": [
      {
          "persona": "gw_user",
          "verbiage": "read all patient data <<purspose>> gw_user"
      },
      {
          "persona": "patient",
          "verbiage": "read all patient data <<purspose>> patient"
      },
      {
          "persona": "test2",
          "verbiage": "added this test"
      }
  ]
};

function CheckPropertiesMatch(obj, keys, property){
  for (var i = 0; i < obj[keys[0]].length; i++){
    if (obj[keys[0]][i][property] == obj[keys[1]][i][property]){
        console.log("Properties at index " + i + " match!");
        } else {
        console.log("Mismatch at index " + i + "!")
    }
  }
}

CheckPropertiesMatch(obj, ['dataText',"purposeText"], "persona")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文