Use SchemaCheck when you already have a JSON-Schema-like object or JSON file and want to validate input against the supported subset instead of writing fluent rules directly.
SchemaCheck is intentionally limited to a reviewable subset. It currently expects the root schema to describe an object.
The current API has three separate steps:
check(input) builds and runs the underlying ObjectCheckresult(options?) reads the last cached result after check(...) or checkResult(...)checkResult(input, options?) runs the schema and returns the formatted result directlyimport { SchemaCheck } from '@samatawy/checks';
const schema = {
type: 'object',
required: ['name', 'age'],
properties: {
name: {
type: 'string',
minLength: 2
},
age: {
type: 'number',
minimum: 18
}
}
};
const schemaCheck = SchemaCheck.from(schema);
const result = await schemaCheck.checkResult({
name: 'Ada',
age: 37
}, {
language: 'en'
});
console.log(result.valid);
Use SchemaCheck.from(schema) when the schema already exists as a TypeScript object in your application.
If you need the underlying fluent validator, call await schemaCheck.check(input) instead and work with the returned ObjectCheck.
import { SchemaCheck } from '@samatawy/checks';
const schemaCheck = SchemaCheck.fromFile('./person.schema.json');
const result = await schemaCheck.checkResult({
name: 'Ada',
age: 37
}, {
flattened: true,
language: 'en'
});
console.log(result.errors);
Use SchemaCheck.fromFile(path) when the schema lives in a JSON file on disk.
The most common supported keywords are:
type, properties, required, additionalProperties: falseitems, minItems, maxItems, contains, minContains, maxContainsminLength, maxLength, pattern, format, enum, constminimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOfallOf, anyOf, oneOf, notExample:
import { SchemaCheck } from '@samatawy/checks';
const schema = {
type: 'object',
required: ['tags'],
properties: {
tags: {
type: 'array',
minItems: 1,
items: {
type: 'string',
minLength: 2
}
}
}
};
const result = await SchemaCheck.from(schema).checkResult({
tags: ['ab', 'cd']
}, {
validated: 'partial',
language: 'en'
});
console.log(result.valid);
console.log(result.validated);
SchemaCheck also supports the current composition subset.
import { SchemaCheck } from '@samatawy/checks';
const schema = {
type: 'object',
required: ['value'],
properties: {
value: {
oneOf: [
{ type: 'string', minLength: 2 },
{ type: 'number', minimum: 10 }
]
}
}
};
const result = await SchemaCheck.from(schema).checkResult({
value: 'ok'
}, {
language: 'en'
});
console.log(result.valid);
Use anyOf, oneOf, and not when you want the schema to express alternatives or exclusions in the same way the fluent API now can.
contains And Related Array KeywordsIn JSON Schema, contains is a keyword, not a method. It means an array is valid when at least one item matches the nested subschema.
Example intent:
{
"type": "array",
"contains": {
"type": "string",
"minLength": 3
}
}
That schema means the array must contain at least one string item whose length is 3 or more.
The closely related keywords are:
minContains: the minimum number of array items that must match the contains subschemamaxContains: the maximum number of array items that may match the contains subschemaprefixItems: positional or tuple-style validation where item 0, item 1, and so on can each have a different schemaunevaluatedItems: rules for array items that were not already covered by items, prefixItems, or composition branchesThese keywords are useful when array validation depends on matching counts or on the position of items, not just on applying one schema to every item.
SchemaCheck now supports contains, minContains, and maxContains.
SchemaCheck still does not implement prefixItems or unevaluatedItems. If one of those keywords appears in the input schema, SchemaCheck throws an unsupported-keyword error instead of partially interpreting it.
checkResult() And result()SchemaCheck.result(options?) and SchemaCheck.checkResult(input, options?) accept the same result options as the fluent validators.
const schemaCheck = SchemaCheck.from(schema);
const output = await schemaCheck.checkResult(input, {
flattened: true,
validated: 'partial',
language: 'en'
});
console.log(output.errors);
console.log(output.validated);
If you already called check(input) or checkResult(input, options?), you can read the cached result again without re-running validation:
await schemaCheck.check(input);
const output = schemaCheck.result({
flattened: true,
language: 'en'
});
console.log(output.errors);
That means you can still choose nested results, flattened messages, or validated output depending on the caller.
SchemaCheck does not implement the full JSON Schema standard.
Important current limits include:
$ref, $defs, and related reference features are not supportedif / then / else are not supportedprefixItems and unevaluatedItems are not supportedprefixItems are not supportedpatternProperties are not supportedFor the broader standards comparison, see json-schema-comparison.md.