1

I have the following schema:

const LIST_EVENTS = {
    "id": "/listEvents",
    "type": "object",
    "properties": {
        "filter": {
            "$ref": "/MarketFilter",
            "required": true
        },
        "locale": {
            "type": "string"
        }
    }
}

From debugging, I can see that the object being sent to the validation is:

{
    marketFilter: {
        eventTypeIds: [ '1' ],
        marketStartTime: {
            from: '2018-12-15T00:00:00+00:00',
            to: '2018-12-15T23:59:59+00:00'
        }
    }
}

marketFilter does not match the name of filter in the schema. To my understanding, seeing as this is a required property, this should have been flagged in the errors array of the validation result but it is not. This is my validation result:

ValidatorResult {
  instance:
    { marketFilter: { eventTypeIds: [Array], marketStartTime: [Object] } },
  schema:
    { id: '/listEvents',
      type: 'object',
      properties: { filter: [Object], locale: [Object] } },
  propertyPath: 'instance',
  errors: [],
  throwError: undefined,
  disableFormat: false }

I thought that it was possible that it did not mind about the naming convention so I removed the property altogether and still, an error is not logged with this being the validation result:

ValidatorResult {
  instance: {},
  schema:
    { id: '/listEvents',
      type: 'object',
      properties: { filter: [Object], locale: [Object] } },
  propertyPath: 'instance',
  errors: [],
  throwError: undefined,
  disableFormat: false }

I have many schemas and they are all added via .addSchema method

wmash
  • 4,032
  • 3
  • 31
  • 69
  • What JSON Schema implementation are you using? You have a JavaScript tag, but there are many libraries. – gregsdennis Dec 15 '18 at 20:27
  • Additionally, you should try moving the `required` out, like I describe in [this answer] (https://stackoverflow.com/a/53454670/878701). Using the most recent schema draft is probably your best bet. – gregsdennis Dec 15 '18 at 20:32

2 Answers2

2

You have two issues with your schema. The main issue is that your required keyword is ignored because it is next to $ref. When an object with a $ref keyword is encountered where a schema is expected, it's treated as a JSON Reference only. It's not treated as a schema. A JSON Reference only has semantics for the $ref keyword. Everything else is ignored. You could fix your problem by isolating the $ref in you schema.

"filter": {
    "allOf": [{ "$ref": "/MarketFilter" }],
    "required": true
}

The other problem is the use of the boolean form of the required keyword. This usage of the required keyword was removed from the JSON Schema specification years ago. Unless you are specifically writing JSON Schemas against the draft-03 specification (unlikely, it's long out of date), you should be using the array form of required. Some older implementations allow you to use both forms, but that's not a good idea. You should be targeting a single specification and not mix keywords from two different versions of specification.

{
    "id": "/listEvents",
    "type": "object",
    "properties": {
        "filter": {
            "$ref": "/MarketFilter"
        },
        "locale": {
            "type": "string"
        }
    },
    "required": ["filter"]
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
0

For now, I have found a work-around that is also described in the docs. I have added the required array property to the schema and added filter to it. This now threw an error.

However, the documentation states that the required property on the property itself should work the same. Is this potentially an issue with the package or is there different behaviours if the property is a reference?

wmash
  • 4,032
  • 3
  • 31
  • 69
  • 2
    can you please delete this answer and post it below your question? This answer is more likely a comment :) – messerbill Dec 15 '18 at 16:57