@hermiforge-decorix/json-schema
v0.4.0
Published
JSON Schema adapter for Decorix metadata.
Maintainers
Readme
@hermiforge-decorix/json-schema

JSON Schema adapter for Decorix metadata. It emits JSON Schema draft 2020-12 objects from decorated classes or builder metadata.
Full usage guide:
docs/(narrative walkthrough beyond this package's API reference).
Install
pnpm add @hermiforge-decorix/core @hermiforge-decorix/json-schemaPeer dependencies: none.
Decorated Class
import {Email, Label, MinLength, Model, Required} from '@hermiforge-decorix/core';
import {toJsonSchema} from '@hermiforge-decorix/json-schema';
@Model('SignupDto')
class SignupDto {
@Required('Name is required')
@MinLength(2, 'Name is too short')
@Label('Name')
name!: string;
@Required('Email is required')
@Email('Invalid email')
email!: string;
}
const schema = toJsonSchema(SignupDto);Builder Model
import {model, stringField} from '@hermiforge-decorix/core';
import {toJsonSchema} from '@hermiforge-decorix/json-schema';
const SignupDto = model('SignupDto', {
name: stringField().required('Name is required').minLength(2, 'Name is too short').label('Name'),
email: stringField().required('Email is required').email('Invalid email')
});
const schema = toJsonSchema(SignupDto);Import (JSON Schema → metadata)
fromJsonSchema performs the best-effort inverse of toJsonSchema: standard
keywords map back to native constraints, and Decorix-specific
x-decorix-constraints entries are restored verbatim, so
toJsonSchema(fromJsonSchema(schema)) is stable for Decorix-produced schemas.
Arbitrary custom validator/predicate functions cannot be reconstructed and are
preserved as the '[function]' sentinel.
import {validate} from '@hermiforge-decorix/core';
import {fromJsonSchema} from '@hermiforge-decorix/json-schema';
const metadata = fromJsonSchema({
title: 'SignupDto',
type: 'object',
properties: {
name: {type: 'string', minLength: 2},
email: {type: 'string', format: 'email'}
},
required: ['name', 'email']
});
validate({name: 'Al', email: '[email protected]'}, metadata); // { success: true, ... }Validator Notes
@hermiforge-decorix/json-schema emits schema data only and does not need a ValidatorAdapter.
Security Note
Only call fromJsonSchema on schemas you trust — generated by toJsonSchema, or
otherwise audited. A pattern keyword (or a RegExp restored from
x-decorix-constraints) becomes a live RegExp that Decorix re-runs on every
validation; a schema from an untrusted source (a third-party upload, an
unreviewed import) could carry a catastrophically backtracking pattern
(ReDoS). fromJsonSchema performs no complexity/length limiting on imported
patterns.
License
LGPL-3.0-or-later — see the repository LICENSE.
