UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

45 lines (44 loc) 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UlidField = exports.ULID_REGEX = void 0; var _utils = require("@backland/utils"); var _FieldType = require("./FieldType"); const ULID_REGEX = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/; exports.ULID_REGEX = ULID_REGEX; class UlidField extends _FieldType.FieldType { constructor(def = {}) { super({ def: def, name: 'ulid' }); const { autoCreate } = def; (0, _utils.expectedType)({ autoCreate }, 'boolean', true); this.parse = this.applyParser({ parse(input) { (0, _utils.expectedType)({ value: input }, 'string'); if (!ULID_REGEX.test(input)) throw new Error('Invalid ulid.'); return input; }, preParse(input) { if (autoCreate && input === undefined) { return (0, _utils.ulid)(); } return input; } }); } static create = def => { return new UlidField(def); }; static isUlid = value => ULID_REGEX.test(value); } exports.UlidField = UlidField; //# sourceMappingURL=UlidField.js.map