UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

158 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaBuilder = void 0; class SchemaBuilder { get str() { return this.String(); } get num() { return this.Number(); } get bool() { return this.Boolean(); } get undef() { return this.Const(undefined); } get nil() { return this.Const(null); } get arr() { return this.Array(this.any); } get obj() { return this.Object(); } get map() { return this.Map(this.any); } get bin() { return this.Binary(this.any); } get any() { return this.Any(); } get fn() { return this.Function(this.any, this.any); } get fn$() { return this.Function$(this.any, this.any); } Boolean(options) { return { ...options, kind: 'bool' }; } Number(options) { return { ...options, kind: 'num' }; } String(options) { return { ...options, kind: 'str' }; } Binary(type, options = {}) { return { ...options, kind: 'bin', type, }; } Array(type, options) { return { ...options, kind: 'arr', type, }; } /** * Use TypeScript const when defining a constant value. * * @example * * ```ts * s.Const('foo' as const); * ``` */ Const(value, options) { return { ...options, kind: 'con', value: value }; } Tuple(head, type, tail) { const schema = { kind: 'arr', head }; if (type) schema.type = type; if (tail) schema.tail = tail; return schema; } Object(...args) { const first = args[0]; if (args.length === 1 && first && typeof first === 'object' && first.keys instanceof Array) return { kind: 'obj', ...first }; if (args.length >= 1 && args[0] instanceof Array) return this.Object({ keys: args[0], ...args[1], }); return this.Object({ keys: args }); } /** Declares an object property. */ Key(key, value, options = {}) { return { ...options, kind: 'key', key, value, }; } /** Declares an optional object property. */ KeyOpt(key, value, options = {}) { return { ...options, kind: 'key', key, value, optional: true, }; } Map(value, key, options) { return { ...(key && { key }), ...options, kind: 'map', value }; } Any(options = {}) { return { ...options, kind: 'any', }; } Ref(ref, options = {}) { return { ...options, kind: 'ref', ref: ref, }; } Or(...types) { return { kind: 'or', types, discriminator: ['num', -1], }; } Function(req, res, options = {}) { return { ...options, kind: 'fn', req, res, }; } Function$(req, res, options = {}) { return { ...options, kind: 'fn$', req, res, }; } } exports.SchemaBuilder = SchemaBuilder; //# sourceMappingURL=SchemaBuilder.js.map