Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useDefaults #758

Closed
Baptiste-Garcin opened this issue Feb 15, 2024 · 3 comments
Closed

useDefaults #758

Baptiste-Garcin opened this issue Feb 15, 2024 · 3 comments

Comments

@Baptiste-Garcin
Copy link

Baptiste-Garcin commented Feb 15, 2024

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
Copy link
Owner

@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
Copy link
Owner

@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
Copy link
Author

Hi !

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

Thanks for the debugging tips !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants