简体   繁体   中英

Json Schema AJV not: const: error message is obscure

I find it very hard to work with the error message from Ajv when using not in my schema. In my use case, I want to validate two props not being of the same value. Consider this example:

schema.json

{
  "$schema": "http://json-schema.org/draft-06/schema",
  "properties": {
    "propA": {
      "type": "string"
    },
    "propB": {
      "type": "string",
      "not": {
        "const": {
          "$data": "1/propA"
        }
      }
    }
  }
}

data

{ propA: 'foo', propB: 'foo' }

output

[ { keyword: 'not',
    dataPath: '.propB',
    schemaPath: '#/properties/propB/not',
    params: {},
    message: 'should NOT be valid' } ]

The error message tells me that not is rejected, but the really relevant information would be that const was not satisfied.

Does anyone have a hint or workaround how I can get a meaningful error message for this use case?

"const" actually was satisfied, and that's why "not" failed. The error object contains paths in data and in schema and with "verbose" option it would contain references to both data and schema that caused the failure; from these you should be able to interpret the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM