natesilva / jayschema

[Unmaintained] - A comprehensive JSON Schema validator for Node.js
BSD 3-Clause "New" or "Revised" License
217 stars 22 forks source link

validate hang #56

Open shachr opened 9 years ago

shachr commented 9 years ago

given the following object and schema the validate function hang:

object:

{
    provider: { name: "1" }
}

schema:

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

        definitions: {
            social: {
                additionalProperties: false,
                type: 'object',
                properties: {
                    provider: { enum: [ "facebook" ], required:true }
                }
            },
            site: {
                additionalProperties: false,
                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" }
                }
            }
        }
    }
natesilva commented 9 years ago

In JSON Schema v4, the required attribute must be an array, and it is a property of the object definition. This was one of the main things that changed from v3. Try this:

var schema = {
    id:"/api/accounts/register",
    type: "object",
    oneOf: [ { $ref:"#/definitions/social" }, { $ref:"#/definitions/site" } ],

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

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

I agree, though, the fact that this hangs is bad and I will take a look preventing that.