Giter Club home page Giter Club logo

Comments (9)

zaggino avatar zaggino commented on August 10, 2024

Hi @burhanfarooq , you can do it like this (see 8c4a6e3)

var schemaForObject = {
        type: "object",
        additionalProperties: false,
        properties: {
            "is_and": { type: "boolean" },
            "filters": {
                type: "array",
                additionalItems: false,
                items: {
                    oneOf: [
                        { $ref: "#" },
                        { $ref: "#/definitions/last" }
                    ]
                }
            }
        },
        definitions: {
            "last": {
                type: "object",
                additionalProperties: false,
                properties: {
                    "text": { type: "string" },
                    "is_last": { type: "boolean" },
                    "filters": { type: "array", additionalItems: false }
                }
            }
        }
    };

from z-schema.

burhanfarooq avatar burhanfarooq commented on August 10, 2024

@zaggino thanks

from z-schema.

burhanfarooq avatar burhanfarooq commented on August 10, 2024

one more question now the error message i am getting in case of wrong property name or mismatch type value of property is not exactly pin pointing the property it just say something like that:


{
  "instance": {
    "is_and": false,
    "filters": [
      {
        "is_and": false,
        "filters": [
          {
            "is_and": true,
            "filters": [
              {
                "is_and": true,
                "filters": [
                  {
                    "is_and": true,
                    "filters": [
                      {
                        "is_and": true,
                        "filters": [
                          {
                            "text": 99,
                            "is_last": true,
                            "filters": []
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "schema": {
    "type": "object",
    "required": true,
    "additionalProperties": false,
    "properties": {
      "is_and": {
        "type": "boolean",
        "required": true
      },
      "filters": {
        "type": "array",
        "additionalItems": false,
        "items": {
          "oneOf": [
            {
              "$ref": "#"
            },
            {
              "$ref": "#/definitions/last"
            }
          ]
        }
      }
    },
    "definitions": {
      "last": {
        "type": "object",
        "required": true,
        "additionalProperties": false,
        "properties": {
          "text": {
            "type": "string"
          },
          "is_last": {
            "type": "boolean"
          },
          "filters": {
            "type": "array",
            "additionalItems": false
          }
        }
      }
    }
  },
  "propertyPath": "my_object",
  "errors": [
    {
      "property": "my_object.filters[0]",
      "message": "is not exactly one from [object Object],[object Object]",
      "schema": {
        "oneOf": [
          {
            "$ref": "#"
          },
          {
            "$ref": "#/definitions/last"
          }
        ]
      },
      "instance": {
        "is_and": false,
        "filters": [
          {
            "is_and": true,
            "filters": [
              {
                "is_and": true,
                "filters": [
                  {
                    "is_and": true,
                    "filters": [
                      {
                        "is_and": true,
                        "filters": [
                          {
                            "text": 99,
                            "is_last": true,
                            "filters": []
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      "stack": "my_object.filters[0] is not exactly one from [object Object],[object Object]"
    },
    {
      "property": "my_object.filters[0]",
      "message": "is not exactly one from [object Object],[object Object]",
      "schema": {
        "oneOf": [
          {
            "$ref": "#"
          },
          {
            "$ref": "#/definitions/last"
          }
        ]
      },
      "instance": {
        "is_and": false,
        "filters": [
          {
            "is_and": true,
            "filters": [
              {
                "is_and": true,
                "filters": [
                  {
                    "is_and": true,
                    "filters": [
                      {
                        "is_and": true,
                        "filters": [
                          {
                            "text": 99,
                            "is_last": true,
                            "filters": []
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      },
      "stack": "my_object.filters[0] is not exactly one from [object Object],[object Object]"
    }
  ]
}

the text property in of last object is number instead of string,
normally the validation points to nested arrays with index and property name but not in this case???

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Please edit you message to fix formatting of the code - it's unreadable.
https://help.github.com/articles/github-flavored-markdown

from z-schema.

burhanfarooq avatar burhanfarooq commented on August 10, 2024

@zaggino code updated have look now please

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

@burhanfarooq this will require a validator fix, i'll look at it soon

from z-schema.

burhanfarooq avatar burhanfarooq commented on August 10, 2024

Roger SIr :)

@zaggino

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Validate like this

ZSchema.validate(invalidObject, schemaForObject, function (err) {
    console.log(JSON.stringify(err, null, 4));
});

You'll get an error like this one, which is fine and can't be really done any better.
Path is good - because there is oneOf, validator needs to go both ways validating complete object below current one.
Reasons why oneOf schemas didn't pass on this level are logged into array subErrors,
When you look at the deepest error you find what you've been looking for

"code": "INVALID_TYPE",
"message": "invalid type: integer (expected string)",
"path": "#/filters/[0]/text",
{
  "errors": [
      {
          "code": "ONE_OF_MISSING",
          "message": "Data does not match any schemas from \"oneOf\"",
          "path": "#/filters/[0]",
          "params": {},
          "subErrors": [
              {
                  "code": "OBJECT_ADDITIONAL_PROPERTIES",
                  "message": "Additional properties not allowed",
                  "path": "#/filters/[0]",
                  "params": {
                      "properties": [
                          "is_and"
                      ]
                  }
              },
              {
                  "code": "ONE_OF_MISSING",
                  "message": "Data does not match any schemas from \"oneOf\"",
                  "path": "#/filters/[0]/filters/[0]",
                  "params": {},
                  "subErrors": [
                      {
                          "code": "OBJECT_ADDITIONAL_PROPERTIES",
                          "message": "Additional properties not allowed",
                          "path": "#/filters/[0]",
                          "params": {
                              "properties": [
                                  "is_and"
                              ]
                          }
                      },
                      {
                          "code": "ONE_OF_MISSING",
                          "message": "Data does not match any schemas from \"oneOf\"",
                          "path": "#/filters/[0]/filters/[0]",
                          "params": {},
                          "subErrors": [
                              {
                                  "code": "OBJECT_ADDITIONAL_PROPERTIES",
                                  "message": "Additional properties not allowed",
                                  "path": "#/filters/[0]",
                                  "params": {
                                      "properties": [
                                          "is_and"
                                      ]
                                  }
                              },
                              {
                                  "code": "ONE_OF_MISSING",
                                  "message": "Data does not match any schemas from \"oneOf\"",
                                  "path": "#/filters/[0]/filters/[0]",
                                  "params": {},
                                  "subErrors": [
                                      {
                                          "code": "OBJECT_ADDITIONAL_PROPERTIES",
                                          "message": "Additional properties not allowed",
                                          "path": "#/filters/[0]",
                                          "params": {
                                              "properties": [
                                                  "is_and"
                                              ]
                                          }
                                      },
                                      {
                                          "code": "ONE_OF_MISSING",
                                          "message": "Data does not match any schemas from \"oneOf\"",
                                          "path": "#/filters/[0]/filters/[0]",
                                          "params": {},
                                          "subErrors": [
                                              {
                                                  "code": "OBJECT_ADDITIONAL_PROPERTIES",
                                                  "message": "Additional properties not allowed",
                                                  "path": "#/filters/[0]",
                                                  "params": {
                                                      "properties": [
                                                          "is_and"
                                                      ]
                                                  }
                                              },
                                              {
                                                  "code": "ONE_OF_MISSING",
                                                  "message": "Data does not match any schemas from \"oneOf\"",
                                                  "path": "#/filters/[0]/filters/[0]",
                                                  "params": {},
                                                  "subErrors": [
                                                      {
                                                          "code": "OBJECT_ADDITIONAL_PROPERTIES",
                                                          "message": "Additional properties not allowed",
                                                          "path": "#/filters/[0]",
                                                          "params": {
                                                              "properties": [
                                                                  "text",
                                                                  "is_last"
                                                              ]
                                                          }
                                                      },
                                                      {
                                                          "code": "INVALID_TYPE",
                                                          "message": "invalid type: integer (expected string)",
                                                          "path": "#/filters/[0]/text",
                                                          "params": {
                                                              "expected": "string",
                                                              "type": "integer"
                                                          }
                                                      }
                                                  ]
                                              }
                                          ]
                                      }
                                  ]
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  ],
  "warnings": []
}

from z-schema.

burhanfarooq avatar burhanfarooq commented on August 10, 2024

@zaggino
thanks (Y)

from z-schema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.