Data schema assistance
Inferring and checking data schemas. Template reference in the DocStudio developer reference.
Public from docstudio/core: inferDataSchema, mergeSchemas, listBindingPaths, resolveSchemaPath, resolveSegments, checkBindings, the dataSchema option of validateTemplate and compile, and the types DataSchema, BindingPath. Decision record: ADR 0017.
Data stays schema-free: compile works on any JSON with no schema. A schema is optional help for authors and tools. Supplying one never changes the compiled tree, the metadata hashes or ok; it only adds warnings.
Schema subset
A DataSchema is a JSON object using a small subset of JSON Schema: type (a name or an array of names from object, array, string, number, integer, boolean, null), properties, required, items and additionalProperties (a schema or false). Every other keyword is ignored. A schema or a part of one with none of type, properties or items is unknown, and nothing under it is ever reported. Schemas are untrusted input: lookups use own properties only and hostile keys are plain data.
inferDataSchema
inferDataSchema(sample, { sampleLimit? }) builds a schema from a JSON value.
- Objects give
type: 'object',propertiesandrequired(keys sorted). - Arrays give
type: 'array'anditems, merged over the firstsampleLimitelements (default 1,000; raise it to scan everything). - Merging unions types (
['null', 'number']), foldsintegerintonumber, unions properties and keeps only keys present in every element asrequired. - Scalars give their type; an empty array has no
items.
mergeSchemas(a, b) is the merge operation, exported for tools that combine samples.
listBindingPaths and resolveSchemaPath
listBindingPaths(schema, { maxPaths = 1000, maxDepth = 16 }) returns { path, type } records for autocomplete, in a stable order (customer, customer.name, items, items[0], items[0].price). Property names that the path syntax cannot express (spaces, dots, __proto__, constructor, prototype) are skipped. Paths are relative to the schema given; to list the paths available inside a repeater, pass the item schema from resolveSchemaPath(schema, 'items[0]').
resolveSchemaPath(schema, 'a.b[0].c') returns the sub-schema or undefined. Only plain relative paths are accepted.
checkBindings
checkBindings(template, schema, { limits? }) returns warnings:
| Code | Condition |
|---|---|
UNKNOWN_BINDING |
A path is absent from the schema. The message suggests the closest property name within two edits |
BINDING_TYPE |
A path bound as text holds only objects or arrays, or a repeater or table source is not an array |
Scopes follow the evaluator: the root schema at the top level; inside a repeater or table row the current schema is the items schema of the source path and $parent climbs outward; $root is the root; $rows is the source array; the of part of an aggregate sees the items of its first argument. $props, $index and $count are not checked. Component bodies are not checked (they are stored separately); the call's props, slot content and everything else in the host template are. Conditions, conditional styles, fallbacks, qr and barcode values, table cells, column visibility, footers and page regions are all covered.
A path that goes through a part the schema does not know produces nothing, so incomplete schemas never create noise. A schema with additionalProperties: false, or one that lists properties, treats an unlisted name as unknown binding.
Through validate and compile
validateTemplate(input, { dataSchema }) and compile({ ..., dataSchema }) append the warnings from checkBindings after the semantic diagnostics. They are warnings, so valid and ok are unaffected, but the warning count in the metadata and the diagnostics list include them.
Performance
Median on macOS (Node 24): inferring from 100,000 rows takes 7 ms with default sampling and 0.71 s with a full scan; checking a 1,555-node template takes 6.8 ms. See validation/artifacts/benchmarks/core-m13-schema-assist.json.