-
Notifications
You must be signed in to change notification settings - Fork 163
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
Comments
@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.
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 |
@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! |
Hi ! This issue might rather have something to do with Thanks for the debugging tips ! |
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 asAnd I instantiated AJV with this options
Using classic JSONSchema, it works like a charm.
Now, I am trying to use
default
in Typebox Schema like this: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 againThe text was updated successfully, but these errors were encountered: