sagold / json-schema-library

Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation
MIT License
175 stars 18 forks source link

Handling definitions in `each`? #22

Closed gersmann closed 1 year ago

gersmann commented 1 year ago

Hi, thank you for your library, it is doing just what I need to. I just ran into a problem with definitions.

My schema:

{
    $schema: "http://json-schema.org/draft-07/schema",
    definitions: {
        object1: {
            type: "object",
            properties: {
                prop1: {
                    type: "string",
                },
                prop2: {
                    type: "string",
                },
            },
            required: ["prop1", "prop2"],
        },
    },
    $ref: "#/definitions/object1",
}

Validating this with

jsonSchema.each(
    {
        prop1: "foo",
        prop2: "foo",
    },
    myCallback
);

gives me:

[
  {
    schema: {
      '$schema': 'http://json-schema.org/draft-07/schema',
      definitions: [Object],
      '$ref': '#/definitions/object1',
      '$defs': [Object]
    },
    value: { prop1: 'foo', prop2: 'foo' },
    pointer: '#'
  },
  {
    schema: {
      type: 'error',
      name: 'UnknownPropertyError',
      code: 'unknown-property-error',
      message: 'Could not find a valid schema for property `#` within object',
      data: [Object]
    },
    value: 'foo',
    pointer: '#/prop1'
  },
  {
    schema: {
      type: 'error',
      name: 'UnknownPropertyError',
      code: 'unknown-property-error',
      message: 'Could not find a valid schema for property `#` within object',
      data: [Object]
    },
    value: 'foo',
    pointer: '#/prop2'
  }
]

This seems to be a problem with definitions, are they not being resolved?

sagold commented 1 year ago

Hi gersman.

What happens if you add a "type" keyword to your root object?

{
  "$schema": "...",
  "type": "object"
  "definitions": ...
gersmann commented 1 year ago

Hi @sagold, same problem unfortunately.

sagold commented 1 year ago

Hi Gersman,

within each, $ref resolution was not supported for root objects. With json-schema-library v7.3.1 this has been fixed and published.

Cheers, sagold

gersmann commented 1 year ago

Hi @sagold, many thanks for the update. Tested it, and it works fine. On a related note, it looks as if getSchema is currently still failing the ref resolution, could there be a similar problem, does the schema need to resolve refs before getSchema can work?

sagold commented 1 year ago

I will have a look

sagold commented 1 year ago

With json-schema-library v7.3.2 getSchema supports $ref resolution on root schemas.

gersmann commented 1 year ago

Thanks a lot @sagold