ExodusMovement / schemasafe

A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
https://npmjs.com/@exodus/schemasafe
MIT License
155 stars 12 forks source link

Is recursion supported? #142

Closed DeanRae closed 3 years ago

DeanRae commented 3 years ago

I'm new to JSON schema validation and am trying to use recursion for an array item type like the example below.

e.g.

{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "person": {
            "type": "object",
            "properties": {
                "name": { "type": "string", "pattern": "^[\s\S]*$" },
                "children": {
                    "type": "array",
                    "items": { "$ref": "#/definitions/person" },
                    "default": []
                }
            }
        }
    },

    "type": "object",

    "properties": {
        "person": { "$ref": "#/definitions/person" }
    }
}

I have tried for days to fix this error but I keep on getting: [requireValidation] type should be specified at #/properties/children/items

Any advice on how to fix this please?

edit: I forgot to mention that I was using the parser api

ChALkeR commented 3 years ago

@DeanRae Sorry for the long delay.

  1. Parser uses strong mode by default, which imposes extra checks on the schema.
  2. There was a misdetection in those extra checks in recursive refs, that is now fixed in v1.0.0-rc.4 (a7a5b557c6e0393bf3a2aeeddd129f1332d5db1d).
  3. It will still (deliberately) fail to compile that schema though, unless additionalProperties and required are specified:
{
    "$schema": "http://json-schema.org/draft-07/schema#",

    "definitions": {
        "person": {
            "type": "object",
            "required": [],
            "additionalProperties": false,
            "properties": {
                "name": { "type": "string", "pattern": "^[\\s\\S]*$" },
                "children": {
                    "type": "array",
                    "items": { "$ref": "#/definitions/person" },
                    "default": []
                }
            }
        }
    },

    "type": "object",

    "required": [],
    "additionalProperties": false,
    "properties": {
        "person": { "$ref": "#/definitions/person" }
    }
}
ChALkeR commented 3 years ago

Also note that \ should be escaped in strings, so in order to write down ^[\s\S]*$ into a js/json string, "^[\\s\\S]*$" should be used.

ChALkeR commented 3 years ago

I'm going to close this as a7a5b55 (included in v1.0.0-rc.4) fixed the issue reported here, and the rest are non-issues.

Please feel free to comment if there are more concerns.