Ajv parse and set undefined keys

Viewed 7

How would I parse an object and make it conform to a schema in Ajv?

I am trying to have a fully deserialized version of a object after passing it through the parser defined with a schema.

Consider the following example:

const schema = {
  properties: {
    foo: { type: 'string' },
    bar: { type: 'string' },
  }
}
const parser = ajv.compileParser(schema)
const serialize = ajv.compileSerializer(schema)
const data = parser(serialize({ foo: "foo" }))

I expect to have this object afterwards:

{
  foo: 'foo',
  bar: undefined, // or null
}

But what I get is:

{
  foo: 'foo',
  bar: 'undefined',
}

So I expect missing keys to be defined in the parsed version but with a default value of undefined or null instead of being set to 'undefined'.

0 Answers
Related