UNPKG

express-cargo

Version:

express middleware for class-based request parsing

225 lines (224 loc) 9.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get Alpha () { return Alpha; }, get Alphanumeric () { return Alphanumeric; }, get Email () { return Email; }, get Equal () { return Equal; }, get IsFalse () { return IsFalse; }, get IsTrue () { return IsTrue; }, get Length () { return Length; }, get Max () { return Max; }, get MaxLength () { return MaxLength; }, get Min () { return Min; }, get MinLength () { return MinLength; }, get NotEqual () { return NotEqual; }, get OneOf () { return OneOf; }, get Prefix () { return Prefix; }, get Range () { return Range; }, get Regexp () { return Regexp; }, get Suffix () { return Suffix; }, get Uuid () { return Uuid; }, get Validate () { return Validate; }, get With () { return With; }, get Without () { return Without; } }); const _types = require("./types"); const _metadata = require("./metadata"); function addValidator(target, propertyKey, rule) { const classMeta = new _metadata.CargoClassMetadata(target); const fieldMeta = classMeta.getFieldMetadata(propertyKey); fieldMeta.addValidator(rule); classMeta.setFieldMetadata(propertyKey, fieldMeta); } function Min(minimum, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'min', (input)=>typeof input === 'number' && input >= minimum, message || `${String(propertyKey)} must be >= ${minimum}`)); }; } function Max(maximum, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'max', (input)=>typeof input === 'number' && input <= maximum, message || `${String(propertyKey)} must be <= ${maximum}`)); }; } function Prefix(prefixText, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'prefix', (val)=>typeof val === 'string' && val.startsWith(prefixText), message || `${String(propertyKey)} must start with ${prefixText}`)); }; } function Suffix(suffixText, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'suffix', (val)=>typeof val === 'string' && val.endsWith(suffixText), message || `${String(propertyKey)} must end with ${suffixText}`)); }; } function Equal(value, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'equal', (val)=>val === value, message || `${String(propertyKey)} must be equal to ${value}`)); }; } function NotEqual(value, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'notEqual', (val)=>val !== value, message || `${String(propertyKey)} must not be equal to ${value}`)); }; } function Range(min, max, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'range', (value)=>typeof value === 'number' && value >= min && value <= max, message || `${String(propertyKey)} must be between ${min} and ${max}`)); }; } function IsFalse(message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'isFalse', (val)=>val === false, message || `${String(propertyKey)} must be false`)); }; } function IsTrue(message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'isTrue', (val)=>val === true, message || `${String(propertyKey)} must be true`)); }; } function Length(value, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'length', (val)=>typeof val === 'string' && val.length === value, message || `${String(propertyKey)} must be ${value} characters`)); }; } function MaxLength(max, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'maxLength', (val)=>typeof val === 'string' && val.length <= max, message || `${String(propertyKey)} must not exceed ${max} characters`)); }; } function MinLength(min, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'minLength', (val)=>typeof val === 'string' && val.length >= min, message || `${String(propertyKey)} must be at least ${min} characters`)); }; } function OneOf(options, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'oneOf', (value)=>options.includes(value), message || `${String(propertyKey)} must be one of ${options.join(', ')}`)); }; } function Validate(validateFn, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'validate', validateFn, message || `${String(propertyKey)} did not pass the provided validation rule.`)); }; } function Regexp(pattern, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'regexp', (value)=>typeof value === 'string' && pattern.test(value), message || `${String(propertyKey)} does not match pattern ${pattern}`)); }; } function Email(message) { return (target, propertyKey)=>{ const DEFAULT_EMAIL_PATTERN = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'email', (value)=>typeof value === 'string' && DEFAULT_EMAIL_PATTERN.test(value), message || `${String(propertyKey)} should be email format`)); }; } const ALPHA_PATTERN = /^[a-zA-Z]+$/; function Alpha(message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'alpha', (value)=>typeof value === 'string' && ALPHA_PATTERN.test(value), message || `${String(propertyKey)} should be alphabetic`)); }; } const uuidPatterns = { all: /^[0-9a-f]{8}-[0-9a-f]{4}-[1345][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, v1: /^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, v3: /^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, v4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, v5: /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i }; function Uuid(version, message) { let regex; let versionLabel; const v = version ? version.replace('v', '') : 'all'; switch(v){ case '1': regex = uuidPatterns.v1; versionLabel = 'v1'; break; case '3': regex = uuidPatterns.v3; versionLabel = 'v3'; break; case '4': regex = uuidPatterns.v4; versionLabel = 'v4'; break; case '5': regex = uuidPatterns.v5; versionLabel = 'v5'; break; default: regex = uuidPatterns.all; versionLabel = 'v1, v3, v4, or v5'; } return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'uuid', (value)=>typeof value === 'string' && regex.test(value), message || `${String(propertyKey)} must be a valid UUID format (${versionLabel})`)); }; } function Alphanumeric(message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'alphanumeric', (value)=>typeof value === 'string' && /^[a-zA-Z0-9]+$/.test(value), message || `${String(propertyKey)} should be alphanumeric`)); }; } function With(fieldName, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'with', (value, instance)=>!(!!value && !instance?.[fieldName]), message || `${String(propertyKey)} requires ${fieldName}`)); }; } function Without(fieldName, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, new _types.ValidatorRule(propertyKey, 'without', (value, instance)=>{ if (!instance) return false; return !(!!value && !!instance?.[fieldName]); }, message || `${String(propertyKey)} cannot exist with ${fieldName}`)); }; }