mafintosh/is-my-json-valid

undefined bypasses validation

Closed this issue · 10 comments

const assert = require('assert')

const validate = require('is-my-json-valid')({
  type: 'string'
})

// Works.
assert(validate(''))
assert(!validate(null))
assert(!validate({}))

// Does not work.
assert(!validate(undefined))

Could it be because the value isn't set as required?

How could it be marked as required?

Only object properties can be optional (and therefore marked as required when necessary)…

JSON schema spec is so confusing 😛

AFAICT, the ability to set the required flag directly in a schema is an is-my-json-valid extension, therefore it breaks my use case 😕

Oh, that's interesting hah - so I always thought that the top level definition was 'object', and then properties had to be appended to it. If the top level is just a single value of string then it perhaps isn't required by default?

I believe there's two ways of defining required values, the required field being the latest. Brain is fudge, bed time now - hope this was somehow helpful haha

no no required: true should be non-recommended behavior (perpaps deprecated?) at worst, don't believe it's a custom extension

The behavior is identical for any type:

const validate = require('is-my-json-valid')({
  type: 'object'
})
assert(validate(undefined))

Maybe it's because undefined is not a valid JSON value?

A JSON value can be an object, array, number, string, true, false, or null.

Hmm, it makes sense but IMHO, validateas a function taking a Javascript value (not a JSON string), I would except it to behave nicely.

@emilbayes Thanks mate!