akidee / schema.js

Sophisticated JSON schema based data validation and adaptation
MIT License
173 stars 13 forks source link

"additionalProperties: false" still allows for additional properties #10

Closed diversario closed 13 years ago

diversario commented 13 years ago

Schema:

Schema.create({
description:"API schema",
type:"object",
fallbacks: Schema.STRICT_FALLBACKS,
properties:{
        data: {type: "object",
               additionalProperties: false,
               required: true,
               properties: { entry_title: {type: "string", required: true},
                             entry_body:  {type: "string", required: true},
                             blog_id:  {type: "integer", required: true},
                           },

              }
}
});

This validates the following JSON:

{data: {entry_title: "Title", entry_body: "Body", blog_id: "245232", zomg: "wtf"}}

Is this an issue or am I missing something?

akidee commented 13 years ago

This correct, because the default fallbacks are tolerant, and additional properties are removed in this mode. Take a look at the fallbacks property in README.

diversario commented 13 years ago

But I did specify fallbacks: Schema.STRICT_FALLBACKS.

diversario commented 13 years ago

I tested README example with integer and string and in that schema strick fallbacks affect validation. How come I can't do the same with my schema?

akidee commented 13 years ago

You need to set fallbacks in every schema that should be handled strictly. For v0.2 I approach this design: https://github.com/akidee/schema.js/issues/11

diversario commented 13 years ago

Ughhh... I don't know what I'm doing wrong here, maybe you could take a look at this? var schemaAdd = Schema.create({ fallbacks: Schema.STRICT_FALLBACKS, description:"Schema", type:"object", additionalProperties: false, properties:{ data: {type: "object", required: true, additionalProperties: false, fallbacks: Schema.STRICT_FALLBACKS, properties: { entry_title: {type: "string", required: true}, entry_body: {type: "string", required: true}, id: {type: "integer", required: true} } } } });

This still validated JSON object with additional properties.

akidee commented 13 years ago

I'm sorry - you are right: It seems that the inner schema still does not strictly validate in v0.1.0a Please use the restruct branch, which will be v0.2.0 soon. In this branch, it works. Some corrections:

 var s = schema.create({
    description:"API schema",
    type:"object",
    fallbacks: schema.Validation.STRICT_FALLBACKS,
    properties:{
            data: {type: "object",
                   additionalProperties: false,
                   fallbacks: schema.Validation.STRICT_FALLBACKS,
                   properties: { entry_title: {type: "string"},
                                 entry_body:  {type: "string"},
                                 blog_id:  {type: "integer"}
                               }

                  }
    }
})

1) Use schema.Validation.STRICT_FALLBACKS 2) required is not available in draft 2, instead there is only optional, which defaults to true

diversario commented 13 years ago

Phew, thank you :) I'll just wait for 0.2.0 then, to be in the clear.