Skip to content

Check if parent is undefined, then check children #106

@benneq

Description

@benneq
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, then bar is 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions