UNPKG

express-cargo

Version:

express middleware for class-based request parsing

146 lines (145 loc) 5.01 kB
import { CargoClassMetadata } from './metadata'; function addValidator(target, propertyKey, rule) { const classMeta = new CargoClassMetadata(target); const fieldMeta = classMeta.getFieldMetadata(propertyKey); fieldMeta.addValidator(rule); classMeta.setFieldMetadata(propertyKey, fieldMeta); } export function min(minimum) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'min', validate: (input)=>typeof input === 'number' && input >= minimum, message: `${String(propertyKey)} must be >= ${minimum}` }); }; } export function max(maximum) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'max', validate: (input)=>typeof input === 'number' && input <= maximum, message: `${String(propertyKey)} must be <= ${maximum}` }); }; } export function prefix(prefixText) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'prefix', validate: (val)=>typeof val === 'string' && val.startsWith(prefixText), message: `${String(propertyKey)} must start with ${prefixText}` }); }; } export function suffix(suffixText) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'suffix', validate: (val)=>typeof val === 'string' && val.endsWith(suffixText), message: `${String(propertyKey)} must end with ${suffixText}` }); }; } export function equal(value) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'equal', validate: (val)=>val === value, message: `${String(propertyKey)} must be equal to ${value}` }); }; } export function notEqual(value) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'notEqual', validate: (val)=>val !== value, message: `${String(propertyKey)} must not be equal to ${value}` }); }; } export function range(min, max) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'range', validate: (value)=>typeof value === 'number' && value >= min && value <= max, message: `${String(propertyKey)} must be between ${min} and ${max}` }); }; } export function isFalse() { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'isFalse', validate: (val)=>val === false, message: `${String(propertyKey)} must be false` }); }; } export function isTrue() { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'isTrue', validate: (val)=>val === true, message: `${String(propertyKey)} must be true` }); }; } export function length(value) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'length', validate: (val)=>typeof val === 'string' && val.length === value, message: `${String(propertyKey)} must be ${value} characters` }); }; } export function maxLength(max) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'maxLength', validate: (val)=>typeof val === 'string' && val.length <= max, message: `${String(propertyKey)} must not exceed ${max} characters` }); }; } export function minLength(min) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'minLength', validate: (val)=>typeof val === 'string' && val.length >= min, message: `${String(propertyKey)} must be at least ${min} characters` }); }; } /** * 속성 값이 주어진 값들 중 하나인지 확인합니다. * @param options 허용되는 값의 배열. */ export function oneOf(options) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'oneOf', validate: (value)=>options.includes(value), message: `${String(propertyKey)} must be one of ${options.join(', ')}` }); }; } export function validate(validateFn, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'validate', validate: validateFn, message: message || `${String(propertyKey)} did not pass the provided validation rule.` }); }; } export function regexp(pattern, message) { return (target, propertyKey)=>{ addValidator(target, propertyKey, { type: 'regexp', validate: (value)=>typeof value === 'string' && pattern.test(value), message: message || `${String(propertyKey)} does not match pattern ${pattern}` }); }; }