-
Notifications
You must be signed in to change notification settings - Fork 32
Open
Description
const data = {
foo: undefined // because it is optional, but it could contain an object:
// foo: {
// bar: 42
// }
}Now I'd like to validate foo.bar (if present), which works fine as long as foo is not undefined. If it's undefined, there'll be an exception in var keys = Object.keys(inputFn()).
Here's a real world example I'd like to use:
- If
foo === undefined, it is VALID. - If
foo === null, it is INVALID: "must not be null" - If
typeof foo !== 'object', it is INVALID: "must be an object" - Now that we're sure that it's an object, we can proceed to validate: If
foo.bar <= 9000, thenbaris INVALID: "must be over 9000!"
Is there some way to get this working?
The result objects then might looks like this:
{
foo: true // if foo === undefined
}
{
foo: ["must not be null"] // if foo === null
}
{
foo: ["must be an object"] // if typeof foo !== 'object'
}
{
foo: {
bar: "must be over 9000!" // if foo.bar <= 9000
}
}
{
foo: {
bar: true // if foo.bar > 9000
}
}
Metadata
Metadata
Assignees
Labels
No labels