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

Not getting all schema errors from optional parent object #224

Open
FMoran opened this issue Oct 21, 2018 · 1 comment
Open

Not getting all schema errors from optional parent object #224

FMoran opened this issue Oct 21, 2018 · 1 comment

Comments

@FMoran
Copy link

FMoran commented Oct 21, 2018

Hi, I hope I am not misleading you.
I saw this use case:
In the schema we have an optional object (one of null, object) with a few children (some optional, some not). If there are a few errors in the object, I would expect to get all of them. But actually I am getting 1 error of type "Data does not match any schemas from 'oneOf'" and in the inner only first error of the children is given (not all the errors of all the children). What I am trying to say is that in some cases I don't get the full list of errors. Should I get it? Thank you very much

I tried to explain it using:


module.exports = {
  description: "Issue #223 - Validating optional child of failed optional father",
  tests: [
    {
      description: "should not pass",
      options: {
        breakOnFirstError: false
      },
      schema: {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "OptionalFatherOptionalSon",
        "type": "object",
        "description": "",
        "properties": {
          "optionalFatherObject": {
            "oneOf": [
              {
                "description": "",
                "$ref": "#/definitions/OptionalFatherObject"
              },
              {
                "type": "null"
              }
            ]
          },
          "anotherField": {
            "description": "",
            "type": "number"
          }
        },
        "required": ["anotherField"],
        "additionalProperties": false,
        "definitions": {
          "OptionalFatherObject": {
            "id": "OptionalFatherObject",
            "type": "object",
            "description": "",
            "properties": {
              "name": {
                "description": "",
                "default": "John",
                "maxLength": 45,
                "type": "string"
              },
              "name2": {
                "description": "",
                "default": "John",
                "maxLength": 45,
                "type": "string"
              },
              "birthUnixTime": {
                "description": "",
                "default": 1415273168,
                "type": "number"
              }
            },
            "required": [
              "name",
              "name2"
            ],
            "additionalProperties": false
          }
        }
      },
      data: {"anotherField":10, optionalFatherObject: {birthUnixTime: 'not_unix_date', name: 5}},
      valid: false,
      after: function (err) {
        expect(err[0].code).toBe("ONE_OF_MISSING");
        expect(err[0].inner.length).toBe(4);
        expect(err[0].inner[0].path).toBe("#/optionalFatherObject/name");
        expect(err[0].inner[0].message).toBe("Expected type string but found type integer");
        expect(err[0].inner[1].path).toBe("#/optionalFatherObject/birthUnixTime");
        expect(err[0].inner[1].message).toBe("Expected type string but found type integer");
        expect(err[0].inner[2].message).toBe("Missing required property: name2");
        expect(err[0].inner[3].message).toBe("Expected type null but found type object");

      }
    },
    {
      description: "should pass",
      options: {
        breakOnFirstError: false
      },
      schema: {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "OptionalFatherOptionalSon",
        "type": "object",
        "description": "",
        "properties": {
          "optionalFatherObject": {
            "description": "",
            "$ref": "#/definitions/OptionalFatherObject"
          },
          "anotherField": {
            "description": "",
            "type": "number"
          }
        },
        "required": ["anotherField"],
        "additionalProperties": false,
        "definitions": {
          "OptionalFatherObject": {
            "id": "OptionalFatherObject",
            "type": "object",
            "description": "",
            "properties": {
              "name": {
                "description": "",
                "default": "John",
                "maxLength": 45,
                "type": "string"
              },
              "name2": {
                "description": "",
                "default": "John",
                "maxLength": 45,
                "type": "string"
              },
              "birthUnixTime": {
                "description": "",
                "default": 1415273168,
                "type": "number"
              }
            },
            "required": [
              "name",
              "name2"
            ],
            "additionalProperties": false
          }
        }
      },
      data: {"anotherField":10, optionalFatherObject: {birthUnixTime: 'not_unix_date', name: 5}},
      valid: false,
      after: function (err) {
        expect(err).toBe([
          {
            "code": "OBJECT_MISSING_REQUIRED_PROPERTY",
            "params": [
              "name2"
            ],
            "message": "Missing required property: name2",
            "path": "#/optionalFatherObject",
            "schemaId": "OptionalFatherOptionalSon"
          },
          {
            "code": "INVALID_TYPE",
            "params": [
              "string",
              "integer"
            ],
            "message": "Expected type string but found type integer",
            "path": "#/optionalFatherObject/name",
            "schemaId": "OptionalFatherOptionalSon"
          },
          {
            "code": "INVALID_TYPE",
            "params": [
              "number",
              "string"
            ],
            "message": "Expected type number but found type string",
            "path": "#/optionalFatherObject/birthUnixTime",
            "schemaId": "OptionalFatherOptionalSon"
          }
        ]);
      }
    }
  ]
};
@FMoran FMoran changed the title Not getting all schema errors from optional father object Not getting all schema errors from optional parent object Oct 21, 2018
@zaggino
Copy link
Owner

zaggino commented Nov 26, 2018

Hi, can you submit a PR for this please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants