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

Parent definitions seem to override children in unexpected ways #161

Closed
RichAyotte opened this issue Aug 8, 2023 · 6 comments
Closed

Parent definitions seem to override children in unexpected ways #161

RichAyotte opened this issue Aug 8, 2023 · 6 comments
Assignees
Labels
question Issues that look for answers.

Comments

@RichAyotte
Copy link

Here's a parser and schema that refers to base schema. The base schema has a single property version and the child type with the allOf requirement of base.

const { parser } = require('@exodus/schemasafe')

const parse = parser(
  {
    $schema: 'https://json-schema.org/draft/2020-12/schema',
    allOf: [
      {
        $ref: '#/definitions/base',
      },
    ],
    definitions: {
      base: {
        required: [],
        properties: {
          version: {
            const: '2',
          },
        },
        type: 'object',
        unevaluatedProperties: false,
      },
    },
    properties: {
      type: {
        const: 'acknowledge',
      },
    },
    required: ['type'],
    type: 'object',
    unevaluatedProperties: false,
  },
  { includeErrors: true, allErrors: true }
)

const validMessage = JSON.stringify({
  type: 'acknowledge',
  version: '2',
})

console.log(parse(validMessage))

I was expecting the following to be valid.

{
  type: 'acknowledge',
  version: '2',
}

Instead, the parser errors with

{
  valid: false,
  error: 'JSON validation failed for unevaluatedProperties at #/type',
  errors: [
    {
      keywordLocation: '#/allOf/0/$ref/unevaluatedProperties',
      instanceLocation: '#/type'
    }
  ]
}

It is valid when I modify as follows

        propertyNames: {
          enum: ['version', 'type'],
        },
        unevaluatedProperties: {
          type: 'string',
          pattern: '^acknowledge$',
        },

Is this the expected behaviour?

@ChALkeR
Copy link
Contributor

ChALkeR commented Aug 8, 2023

According to the spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-11.3, it appears to be dependent only on adjacent in-place applicators (i.e. effectively anything nested inside the schema that contains unevaluatedProperties), so at the place of #/allOf/0/$ref/unevaluatedProperties failure in the ref, type isn't evaluated there (inside that ref).

I'll try to clarify this just in case though. Upd: see testsuite refs in the next comment.

@ChALkeR ChALkeR added the question Issues that look for answers. label Aug 8, 2023
@ChALkeR ChALkeR self-assigned this Aug 8, 2023
@ChALkeR
Copy link
Contributor

ChALkeR commented Aug 8, 2023

Perhaps the proper solution for this would be to lift additionalProperties or unevaluatedProperties should be specified requirement for top-level ref objects which are not used directly, so that can be instead rechecked on top-level.

That would require a non-trivial change in the tracing logic though.

@ChALkeR
Copy link
Contributor

ChALkeR commented Aug 8, 2023

Draft in #162

@RichAyotte
Copy link
Author

Perhaps the proper solution for this would be to lift additionalProperties or unevaluatedProperties should be specified requirement for top-level ref objects which are not used directly, so that can be instead rechecked on top-level.

That would require a non-trivial change in the tracing logic though.

Yes, that would be a good solution. Thanks for the quick response, I barely had time to make tea 🚤

@ChALkeR
Copy link
Contributor

ChALkeR commented Aug 9, 2023

Implemented in @exodus/[email protected].

@ChALkeR ChALkeR closed this as completed Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants