Skip to main content

compile

$compile

Compiles a Schema (or CompiledSchema) into a CompiledSchema ready for use in value processing. If a schema argument is provided, that schema is compiled and the input value is ignored. If no argument is given, the input value itself is treated as the schema to compile — useful when a schema is being passed through as data.

Typically used together with $process to dynamically select and apply a schema.

Parameters

  • schema (Schema|CompiledSchema, optional): The schema to compile. If omitted, the input value is compiled instead.

Example

import { Schema } from '@versionzero/schema';

// Compile a fixed schema and feed it into $process
const portSchema = new Schema('number').validator({$range: {min: 1, max: 65535}});

new Schema('object', {
port: new Schema('any')
.normalizer({$compile: portSchema})
.transformer('$process'),
})

// Compile the input value itself when schemas arrive as data
new Schema('any').transformer(['$compile', '$process'])