Skip to content

Conversation

@fabioanderegg
Copy link
Contributor

I propose the addition of strict object validation:
Validate objects to only allow fields that are referenced in the props dictionary of an object.

const schema = {
    obj: {
        type: "object",
        props: {
            a: "string"
        },
        strict: true
    }
};

console.log(v.validate({ obj: { a: "string" } }, schema));
// Returns: true

console.log(v.validate({ obj: { a: "string", b: "string" } }, schema));
/* Returns an array with errors:
[ { type: 'objectStrict',
    expected: 'b',
    actual: undefined,
    field: 'obj',
    message: 'The object \'obj\' contains invalid keys: b!' } ]
*/

Perhaps someone has a better name than "strict object validation" for this.

The performance impact with npm run bench is not measurable, but adding a separate "strictObject" validator is certainly possible if this is a problem.

@coveralls
Copy link

coveralls commented Nov 12, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 55750cf on fabioanderegg:strict-object into 3f41557 on icebob:master.

@icebob
Copy link
Owner

icebob commented Jan 7, 2019

Nice PR, thanks. Would be good to add a strict property to the root object too (e.g. $$strict: true). What do you think?

@mbaertschi
Copy link
Contributor

@icebob I added the $$strict: true option on schema root level and enhanced the README.md accordingly

@icebob
Copy link
Owner

icebob commented Feb 7, 2019

Could you resolve merge conflicts?

Copy link
Owner

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, thanks! Perf is not affected.

@icebob icebob merged commit c7b6554 into icebob:master Feb 7, 2019
@icebob
Copy link
Owner

icebob commented Feb 7, 2019

@icebob icebob mentioned this pull request Feb 7, 2019
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

Successfully merging this pull request may close these issues.

4 participants