toolmark-monorepo / @toolmark/core / fromJsonSchema
Function: fromJsonSchema()
fromJsonSchema<
T>(schema):StandardSchemaV1<unknown,T> &StandardJSONSchemaV1<unknown,T>
Defined in: packages/core/src/json-schema/from-json-schema.ts:42
Turns a JSON Schema into a Standard Schema validator that also implements Standard JSON Schema, so tools that only have a JSON Schema (DOM-synthesized forms, server-declared tools) validate their input like any other tool, and M1's schema resolution reads the schema back unchanged.
Supported keywords (exact list): type (string or array of types), enum, const, properties, required, additionalProperties (boolean or a schema for undeclared keys), items, minItems, maxItems, uniqueItems, minLength, maxLength (code points), pattern (unanchored, u flag; see below), minimum, maximum, multipleOf, format (date, time, date-time, email, uri), anyOf, oneOf, allOf, $defs, $ref (local #/$defs/<name> only), and the annotations default, title, description (default is not applied: the output is the input value). Every other keyword is ignored. Issue paths are property names and array indexes (numbers).
pattern hardening (ReDoS): a pattern with a backreference (\1, \k<name>), nested quantifiers (a quantified group whose body has a *, +, ?, {n,} or {n,m} quantifier, e.g. (a+)+, (.*a){11}), or a repeated alternation whose branches are not fixed-length literals with distinct first characters (e.g. (a|a)*) is rejected at construction. A string longer than 10000 UTF-16 code units is never run against a pattern; it gets the issue "Value too long for pattern".
Type Parameters
T
T = unknown
Parameters
schema
A draft 2020-12 JSON Schema using the supported subset. It is deep-copied, so later changes to the argument have no effect.
Returns
StandardSchemaV1<unknown, T> & StandardJSONSchemaV1<unknown, T>
A synchronous Standard Schema (vendor 'toolmark') whose jsonSchema.input() and .output() return a deep copy of schema for target 'draft-2020-12' and throw for any other target.
Example
const input = fromJsonSchema<{ title: string }>({
type: 'object',
properties: { title: { type: 'string', minLength: 1 } },
required: ['title'],
})Throws
ToolmarkError schema_conversion_failed when the schema has a $ref other than a resolvable #/$defs/<name>, an invalid or unsafe pattern, or is not a JSON object.