Function compile

  • compile a jtd schema

    If the schema is declared inline and statically, or using the as const definition, then this will infer the type that the schema validates. As a result, typescript will accurately infer that compile({ type: "boolean" }) both guards and fuzzes boolean values.

    Since this must check for extensions of the generic SomeRootSchema, typescript will allow extra properties. To get around this, you can check that it satisfies SomeRootSchema, which will protect to the type inference, but error for extra properties on object literals. However sometimes, this will also cause problems with infinite types.

    Other common errors include:

    • If the guarded type is never, the schema is likely invalid, probably with a a misspelled ref.
    • If the schema produces an infinite type, the schema is likely not constant, and as a result there are an infinite number of types it could validate, since schemas can be infinitely deep.

    Type Parameters

    Parameters

    • schema: S

    Returns CompiledSchema<RootSchemaData<S>, S>

    Example

    const schema = compile({ properties: { bool: { type: "boolean" } } } satisfies SomeRootSchema);