@jsonjoy.com/json-type
Version:
High-performance JSON Pointer implementation
65 lines (64 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateMinMax = exports.validateWithValidator = exports.validateTType = exports.validateTExample = exports.validateDisplay = void 0;
const validateDisplay = ({ title, description, intro }) => {
if (title !== undefined && typeof title !== 'string')
throw new Error('INVALID_TITLE');
if (description !== undefined && typeof description !== 'string')
throw new Error('INVALID_DESCRIPTION');
if (intro !== undefined && typeof intro !== 'string')
throw new Error('INVALID_INTRO');
};
exports.validateDisplay = validateDisplay;
const validateTExample = (example) => {
(0, exports.validateDisplay)(example);
};
exports.validateTExample = validateTExample;
const validateTType = (tType, kind) => {
(0, exports.validateDisplay)(tType);
const { id } = tType;
if (id !== undefined && typeof id !== 'string')
throw new Error('INVALID_ID');
if (tType.kind !== kind)
throw new Error('INVALID_TYPE');
const { examples } = tType;
if (examples) {
if (!Array.isArray(examples))
throw new Error('INVALID_EXAMPLES');
examples.forEach(exports.validateTExample);
}
};
exports.validateTType = validateTType;
const validateWithValidator = ({ validator }) => {
if (validator !== undefined) {
if (Array.isArray(validator)) {
for (const v of validator)
if (typeof v !== 'string')
throw new Error('INVALID_VALIDATOR');
}
else if (typeof validator !== 'string')
throw new Error('INVALID_VALIDATOR');
}
};
exports.validateWithValidator = validateWithValidator;
const validateMinMax = (min, max) => {
if (min !== undefined) {
if (typeof min !== 'number')
throw new Error('MIN_TYPE');
if (min < 0)
throw new Error('MIN_NEGATIVE');
if (min % 1 !== 0)
throw new Error('MIN_DECIMAL');
}
if (max !== undefined) {
if (typeof max !== 'number')
throw new Error('MAX_TYPE');
if (max < 0)
throw new Error('MAX_NEGATIVE');
if (max % 1 !== 0)
throw new Error('MAX_DECIMAL');
}
if (min !== undefined && max !== undefined && min > max)
throw new Error('MIN_MAX');
};
exports.validateMinMax = validateMinMax;