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

no (or more than one) schemas match - not detailed enough #82

Open
shachr opened this issue Jun 13, 2015 · 6 comments
Open

no (or more than one) schemas match - not detailed enough #82

shachr opened this issue Jun 13, 2015 · 6 comments

Comments

@shachr
Copy link

shachr commented Jun 13, 2015

Given the following schema:

{
        id:"/api/accounts/register",
        additionalProperties: false,
        oneOf: [ { $ref:"#/definitions/social"}, { $ref:"#/definitions/site"} ],

        definitions: {
            social: {
                additionalProperties: false,
                required: true,
                type: 'object',
                properties: {
                    provider: { enum: [ "facebook" ], required:true }
                }
            },
            site: {
                additionalProperties: false,
                required: true,
                type: 'object',
                properties: {
                    username: { type: "string", required:true },
                    password: { type: "string", required:true },

                    firstname: { type: "string" },
                    lastname: { type: "string" },
                    email: { type: "string" },
                    isSubscribed: { type: "boolean" }
                }
            }
        }

    }

i get the following validation error:

[
      {
        "field": "data",
        "message": "no (or more than one) schemas match",
        "value": {

        }
      }
    ]

i would expect to get the following or something similar which is much more useful:

[
      {
        "field": "data",
        "message": "no (or more than one) schemas match",
        "value": {

        }
      },

     {
        "field": "data",
        "type": "oneOf",
        "message": "one of",
        "value": [
             { "message": "is required", "field": ["username","password"]  },
             { "message": "is required", "field": ["provider"]  },
        ]
     }
    ]

this will allow me to show the user that either ( username and password ) or (provider) are missing.

@shachr shachr changed the title no (or more than one) schemas match no (or more than one) schemas match - not detailed enough Jun 13, 2015
@mattanbi
Copy link

Hi, does anyone know the status of this issue? I get the same response.

@mafintosh
Copy link
Owner

i agree that this should be better. this was the easiest way to implement it when i originally did it. if anyone wants to do a pr to fix this let me know

@mattanbi
Copy link

I'll be happy to. Can you shortly explain the validate and error methods so I understand what should be changed there?
if (node.oneOf && node.oneOf.length) {
var prev = gensym('prev')
var passes = gensym('passes')

            validate
            ('var %s = errors', prev)
            ('var %s = 0', passes)

            node.oneOf.forEach(function(sch, i) {
                visit(name, sch, false, false)
                validate('if (%s === errors) {', prev)
                ('%s++', passes)
                ('} else {')
                ('errors = %s', prev)
                ('}')
            })

            validate('if (%s !== 1) {', passes)
            error('no (or more than one) schemas match')
            validate('}')
        }

@mafintosh
Copy link
Owner

@mattanbi basically what that snippet does it that goes through all the schemas defined by the oneOf and checks if that schema matches. If more and 1 or 0 schemas match it'll produce an error by calling the error function. it ignores the actual error messages produced by the subschemas since as long as a single schema from the oneOf matches everything is fine

@mattanbi
Copy link

Good, thanks. Can you point out how I get the errors (that are currently ignored, as you say)? Because when I debug the validate method doesn't return anything.
I don't fully understand where it is actually determent that there is an error.

@singhs020
Copy link

I am also facing same issue and tried something to fix it. Here is the pr
#91

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

No branches or pull requests

4 participants