UNPKG

@opra/common

Version:
72 lines (71 loc) 2.24 kB
import { __decorate, __metadata } from "tslib"; import { toString, vg } from 'valgen'; import { DATATYPE_METADATA, DECODER, ENCODER } from '../../constants.js'; import { SimpleType } from '../simple-type.js'; let StringType = class StringType { constructor(properties) { if (properties) Object.assign(this, properties); } pattern; patternName; minLength; maxLength; [DECODER](properties) { const x = []; if (properties.pattern) { let formatName = properties.patternName; if (!formatName) { const meta = Reflect.getMetadata(DATATYPE_METADATA, Object.getPrototypeOf(this).constructor); formatName = meta.name; } x.push(vg.matches(properties.pattern, { formatName })); } if (properties.minLength) x.push(vg.lengthMin(properties.minLength)); if (properties.maxLength) x.push(vg.lengthMax(properties.maxLength)); return x.length > 0 ? vg.pipe([toString, ...x], { returnIndex: 0 }) : toString; } [ENCODER](properties) { return this[DECODER](properties); } }; __decorate([ SimpleType.Attribute({ description: 'Regex pattern to be used for validation', }), __metadata("design:type", Object) ], StringType.prototype, "pattern", void 0); __decorate([ SimpleType.Attribute({ description: 'Name of the pattern', }), __metadata("design:type", String) ], StringType.prototype, "patternName", void 0); __decorate([ SimpleType.Attribute({ description: 'Minimum number of characters', }), __metadata("design:type", Number) ], StringType.prototype, "minLength", void 0); __decorate([ SimpleType.Attribute({ description: 'Minimum number of characters', }), __metadata("design:type", Number) ], StringType.prototype, "maxLength", void 0); StringType = __decorate([ SimpleType({ name: 'string', description: 'A sequence of characters', nameMappings: { js: 'string', json: 'string', }, }), __metadata("design:paramtypes", [Object]) ], StringType); export { StringType };