sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.77k stars 152 forks source link

useDefaults #758

Closed Baptiste-Garcin closed 6 months ago

Baptiste-Garcin commented 6 months ago

Hi

I am using Typebox with AJV and I am facing a strange behavior.

I am currently migrating JSON Schemas to typebox schema. In my old plain JSON Schema, I use default keyword such as

{
  "version": {
    "type": "string",
    "default": "1.0.0"
  },
}

And I instantiated AJV with this options

const ajv = new Ajv({
  allErrors: true,
  coerceTypes: true,
  schemas, // partly JSON Schema, partly Typebox schema
  useDefaults: true,
});

Using classic JSONSchema, it works like a charm.

Now, I am trying to use default in Typebox Schema like this:

const fooSchema = Type.Intersect([
  commonSchema,
  Type.Object({
    id: Type.Enum(Enum, {default: Enum.Bar})
  })
])

And in this cas, I face this error : Error: strict mode: default is ignored for: data.id

If I pass the options strict: false, it works again

sinclairzx81 commented 6 months ago

@Baptiste-Garcin Hi,

This is more a Ajv question than a TypeBox one. However based on the information you've provided, the following seems to work fine in Ajv strict mode.

import { Type } from "@sinclair/typebox";
import Ajv from 'ajv'

enum Enum {
  A,
  B
}
const Common = Type.Object({
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number()
})
const Foo = Type.Intersect([
  Common,
  Type.Object({
    id: Type.Enum(Enum, { default: Enum.B })
  })
])

// ... test

const ajv = new Ajv({
  allErrors: true,
  coerceTypes: true,
  useDefaults: true,
})

const data = { x: 1, y: 2, z: 3 }

ajv.validate(Foo, data)

console.log(data) // { x: 1, y: 2, z: 3, id: 1 } - id generated with default

There shouldn't technically be an issue here (there's likely something else going on). Remember, TypeBox returns Json Schema, so you can check what it is generating with console.log.

const T = Type.String({ default: 'hello' })

console.log(JSON.stringify(T, null, 2)) // { type: 'string', default: 'hello' }

You may want to verify the schematics are what you expect them to be.

schemas, // partly JSON Schema, partly Typebox schema

Just note that it is fine to mix and match raw Json Schema with TB types if you're using Ajv. This shouldn't be an issue.

Hope this helps S

sinclairzx81 commented 6 months ago

@Baptiste-Garcin Heya

Going to close this issue as it seems to relate more to Ajv configuration than TypeBox. Just check the schematics you have are what you expect as per above. If you're still running into issues, if you can paste a full reproduction of the issue, I can take a look. Just reply on this thread.

Cheers! S

Baptiste-Garcin commented 6 months ago

Hi !

This issue might rather have something to do with defaults and AnyOf in AJV.

Thanks for the debugging tips !