sinclairzx81 / typebox

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

TypeCompiler compile on Type.Strict(T) - Preflight validation check failed to guard for the given schema #928

Closed elvince closed 1 month ago

elvince commented 2 months ago

Hi,

Following this code:

const PostTestBodySchema = Type.Object({
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number(),
});

const PostTestBodySchema2 = Type.Strict( Type.Object({
  x: Type.Number(),
  y: Type.Number(),
  z: Type.Number(),
}));

const PostTestBodyChecker = TypeCompiler.Compile(PostTestBodySchema);
const PostTestBodyChecker2 = TypeCompiler.Compile(PostTestBodySchema2);

We got an erro : 'Preflight validation check failed to guard for the given schema' for PostTestBodyChecker2.

Why Type.Strict make the TypeCompile failed?

thanks,

sinclairzx81 commented 1 month ago

@elvince Hi, Apologies for the delay in reply.

The Type.Strict() function removes compositing symbols from TypeBox types. It is used in scenarios where Json Schema validators reject additional symbols properties passed on schematics. In essence, the Strict function will render a TypeBox type no longer capable with TypeBox infrastructure, so shouldn't be used unless your using a validator that mandates it.

The Strict function is a legacy function in TypeBox (which has historically has been leveraged by large , if you use the TypeCompiler, you can ignore this function.

Hope this helps, and again, apologies for the delay. S

elvince commented 1 month ago

@sinclairzx81 Thanks for the explanation. Now I know that I must create 2 types depanding of my use cases. FYI I need to use the strict type to use it in OpenAPI schema description as I need JsonSchema that conforms to OpenApi V3 specs.

Unless there is another way to do it, I will stick with Strict. Thanks,