@jsonjoy.com/json-type
Version:
High-performance JSON Pointer implementation
97 lines • 2.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbsType = void 0;
const value_1 = require("../../value");
class AbsType {
constructor(schema) {
this.schema = schema;
this._validators = [];
}
sys(system) {
this.system = system;
return this;
}
getSystem() {
const system = this.system;
if (!system)
throw new Error('NO_SYSTEM');
return system;
}
kind() {
return this.schema.kind;
}
value(data) {
return new value_1.Value(data, this);
}
/**
* @todo Add ability to export the whole schema, including aliases.
*/
getSchema() {
return this.schema;
}
/**
* Sets a custom runtime validator for this type.
*
* @param validator Function that validates the value of this type.
* @returns `this` for chaining.
*/
validator(validator, name) {
this._validators.push([validator, name]);
return this;
}
options(options) {
// biome-ignore lint: unused variables are intentional
const { kind, ...sanitizedOptions } = options;
Object.assign(this.schema, sanitizedOptions);
return this;
}
title(title) {
this.schema.title = title;
return this;
}
intro(intro) {
this.schema.intro = intro;
return this;
}
description(description) {
this.schema.description = description;
return this;
}
default(value) {
this.schema.default = value;
return this;
}
example(value, title, options) {
var _a;
const examples = ((_a = this.schema).examples ?? (_a.examples = []));
const example = { ...options, value };
if (typeof title === 'string')
example.title = title;
examples.push(example);
return this;
}
getOptions() {
// biome-ignore lint: unused variables are intentional
const { kind, ...options } = this.schema;
return options;
}
alias(name) {
return this.getSystem().alias(name, this);
}
toStringTitle() {
return this.kind();
}
toStringOptions() {
const options = this.getOptions();
const title = options.title || options.intro || options.description;
if (!title)
return '';
return JSON.stringify(title);
}
toString(tab = '') {
const options = this.toStringOptions();
return this.toStringTitle() + (options ? ` ${options}` : '');
}
}
exports.AbsType = AbsType;
//# sourceMappingURL=AbsType.js.map