ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.72k stars 872 forks source link

Referencing to key's value within schema #2264

Open ktn05 opened 1 year ago

ktn05 commented 1 year ago

Hi,

my object is like below as per schema:

    {
      "name": "new_york",       // new_york
      "address": {
        "type": "type1",
        "title": "adsas sadfd",
        "propertyValue": {
          "new_york":          // need to access dynamic value 'new_york' by refering to "name" field available on top
           {
           type: 'type2'
          }
        },
        "key": "new_york"
      }
    }

and My schema is as follows:

{
  name: {
    type: 'string'
  },
  address: {
    type: 'object',
    properties: {
      title: {
        const: { $data: '2/label' },
        errorMessage: {
          const: 'must be equal to ${2/label}'
        }
      },
      key: {
        const: { $data: '2/name' },
        errorMessage: {
          const: 'must be equal to ${2/name}'
        }
      },
      propertyValue: {
        type: 'object',                                         // here that dynamic validation must happen
        additionalProperties: true
      },
    },
    required: ['type', 'title', 'key', 'properties', 'required'],
    additionalProperties: {
      not: true,
      errorMessage: 'must NOT have additional property ${0#}'
    }
  }
}

So, propertyValue object contains name field value(i.e new_york) in the object which is dynamic "propertyValue": { "new_york": { type: 'type2' } }

But How can validate in json schema that propertyValue object contains name field value(i.e new_york) like i have done under key field object i.e $data: '2/name'?

jasoniangreen commented 1 month ago

Sorry but I don't think this is supported by AJV or JSON Schema. I have looked at the docs for $data and can't see any way it could be used to link to a property name / key.