diff --git a/Sample/src/index.ts b/Sample/src/index.ts index aa4e3c1..ad9f414 100644 --- a/Sample/src/index.ts +++ b/Sample/src/index.ts @@ -168,28 +168,36 @@ const config: { [input: string]: string[] } = {}; const addValidator = (input: string, type: string) => { config[input] = config[input] ? [...config[input], type] : [type]; - - console.log(input, type); }; const Required = (_: any, prop: string) => { addValidator(prop, "required"); }; -const Maxlength = (_: any, prop: string) => addValidator(prop, "maxlength"); + const Positive = (_: any, prop: string) => addValidator(prop, "positive"); +let lengthNumber: number; + +function Length(length: number) { + return function (_: any, property: string) { + addValidator(property, "length"); + + lengthNumber = length; + }; +} + const validate = (course: any) => Object.entries(config).every(([input, types]) => types.every( (type) => (type === "required" && course[input]) || (type === "positive" && course[input] > 0) || - (type === "maxlength" && course[input].length < 5) + (type === "length" && course[input].length < lengthNumber) ) ); class Course { - @Required @Maxlength title: string; + @Required @Length(4) title: string; @Required @Positive price: number; constructor(title: string, price: number) {