Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate object value against array of objects #2282

Closed
tanyamadaan opened this issue May 24, 2023 · 2 comments
Closed

Validate object value against array of objects #2282

tanyamadaan opened this issue May 24, 2023 · 2 comments

Comments

@tanyamadaan
Copy link

I am trying to create ajv based json schema validations. I want to validate if page.id value is equal to any one navigation.ids present in an array. For example:

`"page": {
    "id": "1"
},
"navigation": [
    {"id": "1"},
    {"id": "2"},
    {"id": "3"}
]`

Can anyone please help suggest how can I validate this using ajv.

I was able to check if page.id is equal to first/second/third element in array, but if I specifically mention the element index while checking. However, the requirement is to check if id is equal to any of the element inside the array. Note: the size of navigation array is not limited or fixed.

`page: {
    type: "object",
    properties: {
      id: {
        type: "string",
        const: { $data: "/navigation/0/id"},
      }
`
@webdev-ashishk
Copy link

Try This out ->

{
"type": "object",
"properties": {
"id": {
"type": "string",
"contains": {
"const": { "$data": "/navigation/*/id" }
}
}
}
}

@jasoniangreen
Copy link
Collaborator

Closing as a potential solution has been offered with no reply for more than 2 weeks. In future it can really help the diagnoses of problems if you provide full code samples or even better a runkit example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants