Comparing version 2.2.4 to 2.3.0
@@ -10,3 +10,3 @@ "use strict"; | ||
true() { | ||
this.addInternalTestFunction((b) => b, { | ||
this.addNonRequiredInternalTestFunction((b) => b, { | ||
title: 'not true', | ||
@@ -18,3 +18,3 @@ description: 'must be true', | ||
false() { | ||
this.addInternalTestFunction((b) => !b, { | ||
this.addNonRequiredInternalTestFunction((b) => !b, { | ||
title: 'not false', | ||
@@ -21,0 +21,0 @@ description: 'must be false', |
@@ -10,3 +10,3 @@ "use strict"; | ||
min(min) { | ||
this.addInternalTestFunction((n) => n >= min, { | ||
this.addNonRequiredInternalTestFunction((n) => n >= min, { | ||
title: 'less than minimum', | ||
@@ -22,3 +22,3 @@ description: `must be greater than the minimum value of ${min}`, | ||
openMin(min) { | ||
this.addInternalTestFunction((n) => n > min, { | ||
this.addNonRequiredInternalTestFunction((n) => n > min, { | ||
title: 'less than or equal to minimum', | ||
@@ -30,3 +30,3 @@ description: `must be greater than the open minimum value of ${min}`, | ||
max(max) { | ||
this.addInternalTestFunction((n) => n <= max, { | ||
this.addNonRequiredInternalTestFunction((n) => n <= max, { | ||
title: 'greater than maximum', | ||
@@ -42,3 +42,3 @@ description: `must be less than the maximum value of ${max}`, | ||
openMax(max) { | ||
this.addInternalTestFunction((n) => n < max, { | ||
this.addNonRequiredInternalTestFunction((n) => n < max, { | ||
title: 'greater than or equal to maximum', | ||
@@ -50,3 +50,3 @@ description: `must be less than the open maximum value of ${max}`, | ||
closedInterval(min, max) { | ||
this.addInternalTestFunction((n) => n >= min && n <= max, { | ||
this.addNonRequiredInternalTestFunction((n) => n >= min && n <= max, { | ||
title: 'outside closed interval', | ||
@@ -58,3 +58,3 @@ description: `outside of the closed interval of [${min},${max}]`, | ||
openInterval(min, max) { | ||
this.addInternalTestFunction((n) => n > min && n < max, { | ||
this.addNonRequiredInternalTestFunction((n) => n > min && n < max, { | ||
title: 'outside open interval', | ||
@@ -61,0 +61,0 @@ description: `outside of the open interval of (${min},${max})`, |
@@ -10,2 +10,3 @@ import { DefaultValidationRuleError, RuleTest, ValidationRuleResult } from '../types/ValidationRule'; | ||
export declare class Rule<T> { | ||
protected static valueIsEmpty(value: any): boolean; | ||
protected readonly opts: RuleOptions; | ||
@@ -18,7 +19,10 @@ protected readonly testRunner: RuleTestRunner<T>; | ||
description(d: string): this; | ||
required(): this; | ||
addTestFunction(test: RuleTest<T>): this; | ||
addNonRequiredTestFunction(test: RuleTest<T>): this; | ||
test(data: T): ValidationRuleResult; | ||
protected addInternalTestFunction(test: RuleTest<T>, defaultError?: DefaultValidationRuleError): this; | ||
protected addNonRequiredInternalTestFunction(test: RuleTest<T>, defaultError?: DefaultValidationRuleError): this; | ||
private getFieldNameOrEmpty; | ||
private resultIsError; | ||
} |
@@ -10,2 +10,5 @@ "use strict"; | ||
} | ||
static valueIsEmpty(value) { | ||
return value === undefined || value === null; | ||
} | ||
title(t) { | ||
@@ -19,2 +22,9 @@ this.internalTitle = t; | ||
} | ||
required() { | ||
this.addInternalTestFunction((data) => !Rule.valueIsEmpty(data), { | ||
title: 'required', | ||
description: `is required to have a value`, | ||
}); | ||
return this; | ||
} | ||
addTestFunction(test) { | ||
@@ -24,2 +34,7 @@ this.testRunner.addTest({ test }); | ||
} | ||
addNonRequiredTestFunction(test) { | ||
const combinedTest = (data) => Rule.valueIsEmpty(data) || test(data); | ||
this.addTestFunction(combinedTest); | ||
return this; | ||
} | ||
test(data) { | ||
@@ -43,2 +58,7 @@ const validationRuleResult = this.testRunner.run(data); | ||
} | ||
addNonRequiredInternalTestFunction(test, defaultError) { | ||
const combinedTest = (data) => Rule.valueIsEmpty(data) || test(data); | ||
this.addInternalTestFunction(combinedTest, defaultError); | ||
return this; | ||
} | ||
getFieldNameOrEmpty() { | ||
@@ -45,0 +65,0 @@ return (this.opts.fieldName) ? `${this.opts.fieldName} ` : ''; |
@@ -10,3 +10,3 @@ "use strict"; | ||
minLength(min) { | ||
this.addInternalTestFunction((str) => str && str.length >= min, { | ||
this.addNonRequiredInternalTestFunction((str) => str && str.length >= min, { | ||
title: 'too short', | ||
@@ -18,3 +18,3 @@ description: `must be at least ${min} characters long`, | ||
maxLength(max) { | ||
this.addInternalTestFunction((str) => !str || str.length <= max, { | ||
this.addNonRequiredInternalTestFunction((str) => !str || str.length <= max, { | ||
title: 'too long', | ||
@@ -26,3 +26,3 @@ description: `must not be longer than ${max} characters long`, | ||
blacklist(list) { | ||
this.addInternalTestFunction((str) => !list.some((item) => str.includes(item)), { | ||
this.addNonRequiredInternalTestFunction((str) => !list.some((item) => str.includes(item)), { | ||
title: 'failed blacklist', | ||
@@ -34,3 +34,3 @@ description: `must not contain one of the blacklisted phrases '${list.join('\', \'')}'`, | ||
upperCase() { | ||
this.addInternalTestFunction((str) => !/[a-z]/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => !/[a-z]/.test(str), { | ||
title: 'contains lowercase', | ||
@@ -42,3 +42,3 @@ description: 'must not contain lowercase characters', | ||
lowerCase() { | ||
this.addInternalTestFunction((str) => !/[A-Z]/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => !/[A-Z]/.test(str), { | ||
title: 'contains uppercase', | ||
@@ -50,3 +50,3 @@ description: 'must not contain uppercase characters', | ||
alphanumeric() { | ||
this.addInternalTestFunction((str) => /^[a-zA-Z0-9]*$/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => /^[a-zA-Z0-9]*$/.test(str), { | ||
title: 'not alphanumeric', | ||
@@ -58,3 +58,3 @@ description: 'must only contain letters and numbers', | ||
regex(regex) { | ||
this.addInternalTestFunction((str) => regex.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => regex.test(str), { | ||
title: 'failed regex', | ||
@@ -61,0 +61,0 @@ description: `failed the regular expression '${regex}'`, |
@@ -87,4 +87,11 @@ "use strict"; | ||
}); | ||
it('should pass', () => { | ||
const schema = new Schema_1.Schema() | ||
.addField('required', (f) => f | ||
.string() | ||
.addRule((r) => r.minLength(4)) | ||
.addRule((r) => r.maxLength(10))); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=Schema.spec.js.map |
@@ -17,3 +17,3 @@ import { ValidationRuleResult } from '../types/ValidationRule'; | ||
public true(): this { | ||
this.addInternalTestFunction((b) => b, { | ||
this.addNonRequiredInternalTestFunction((b) => b, { | ||
title: 'not true', | ||
@@ -26,3 +26,3 @@ description: 'must be true', | ||
public false(): this { | ||
this.addInternalTestFunction((b) => !b, { | ||
this.addNonRequiredInternalTestFunction((b) => !b, { | ||
title: 'not false', | ||
@@ -29,0 +29,0 @@ description: 'must be false', |
@@ -17,3 +17,3 @@ import { ValidationRuleResult } from '../types/ValidationRule'; | ||
public min(min: number): this { | ||
this.addInternalTestFunction((n) => n >= min, { | ||
this.addNonRequiredInternalTestFunction((n) => n >= min, { | ||
title: 'less than minimum', | ||
@@ -31,3 +31,3 @@ description: `must be greater than the minimum value of ${min}`, | ||
public openMin(min: number): this { | ||
this.addInternalTestFunction((n) => n > min, { | ||
this.addNonRequiredInternalTestFunction((n) => n > min, { | ||
title: 'less than or equal to minimum', | ||
@@ -40,3 +40,3 @@ description: `must be greater than the open minimum value of ${min}`, | ||
public max(max: number): this { | ||
this.addInternalTestFunction((n) => n <= max, { | ||
this.addNonRequiredInternalTestFunction((n) => n <= max, { | ||
title: 'greater than maximum', | ||
@@ -54,3 +54,3 @@ description: `must be less than the maximum value of ${max}`, | ||
public openMax(max: number): this { | ||
this.addInternalTestFunction((n) => n < max, { | ||
this.addNonRequiredInternalTestFunction((n) => n < max, { | ||
title: 'greater than or equal to maximum', | ||
@@ -63,3 +63,3 @@ description: `must be less than the open maximum value of ${max}`, | ||
public closedInterval(min: number, max: number): this { | ||
this.addInternalTestFunction((n) => n >= min && n <= max, { | ||
this.addNonRequiredInternalTestFunction((n) => n >= min && n <= max, { | ||
title: 'outside closed interval', | ||
@@ -72,3 +72,3 @@ description: `outside of the closed interval of [${min},${max}]`, | ||
public openInterval(min: number, max: number): this { | ||
this.addInternalTestFunction((n) => n > min && n < max, { | ||
this.addNonRequiredInternalTestFunction((n) => n > min && n < max, { | ||
title: 'outside open interval', | ||
@@ -75,0 +75,0 @@ description: `outside of the open interval of (${min},${max})`, |
import { | ||
DefaultValidationRuleError, | ||
RuleTest, TestAndOptionalDefaultError, | ||
RuleTest, | ||
ValidationRuleError, | ||
@@ -18,2 +18,6 @@ ValidationRuleResult, ValidationRuleSuccess, | ||
export class Rule<T> { | ||
protected static valueIsEmpty(value: any): boolean { | ||
return value === undefined || value === null; | ||
} | ||
protected readonly opts: RuleOptions; | ||
@@ -39,2 +43,10 @@ protected readonly testRunner: RuleTestRunner<T>; | ||
public required(): this { | ||
this.addInternalTestFunction((data: any) => !Rule.valueIsEmpty(data), { | ||
title: 'required', | ||
description: `is required to have a value`, | ||
}); | ||
return this; | ||
} | ||
public addTestFunction(test: RuleTest<T>): this { | ||
@@ -45,2 +57,7 @@ this.testRunner.addTest({ test }); | ||
public addNonRequiredTestFunction(test: RuleTest<T>): this { | ||
const combinedTest = (data: any) => Rule.valueIsEmpty(data) || test(data); | ||
this.addTestFunction(combinedTest); | ||
return this; | ||
} | ||
public test(data: T): ValidationRuleResult { | ||
@@ -65,2 +82,8 @@ const validationRuleResult: ValidationRuleResult = this.testRunner.run(data); | ||
protected addNonRequiredInternalTestFunction(test: RuleTest<T>, defaultError?: DefaultValidationRuleError): this { | ||
const combinedTest = (data: any) => Rule.valueIsEmpty(data) || test(data); | ||
this.addInternalTestFunction(combinedTest, defaultError); | ||
return this; | ||
} | ||
private getFieldNameOrEmpty(): string { | ||
@@ -67,0 +90,0 @@ return (this.opts.fieldName) ? `${this.opts.fieldName} ` : ''; |
@@ -17,3 +17,3 @@ import { ValidationRuleResult } from '../types/ValidationRule'; | ||
public minLength(min: number): this { | ||
this.addInternalTestFunction((str) => str && str.length >= min, { | ||
this.addNonRequiredInternalTestFunction((str) => str && str.length >= min, { | ||
title: 'too short', | ||
@@ -26,3 +26,3 @@ description: `must be at least ${min} characters long`, | ||
public maxLength(max: number): this { | ||
this.addInternalTestFunction((str) => !str || str.length <= max, { | ||
this.addNonRequiredInternalTestFunction((str) => !str || str.length <= max, { | ||
title: 'too long', | ||
@@ -35,3 +35,3 @@ description: `must not be longer than ${max} characters long`, | ||
public blacklist(list: string[]): this { | ||
this.addInternalTestFunction((str) => !list.some((item) => str.includes(item)), { | ||
this.addNonRequiredInternalTestFunction((str) => !list.some((item) => str.includes(item)), { | ||
title: 'failed blacklist', | ||
@@ -44,3 +44,3 @@ description: `must not contain one of the blacklisted phrases '${list.join('\', \'')}'`, | ||
public upperCase(): this { | ||
this.addInternalTestFunction((str) => !/[a-z]/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => !/[a-z]/.test(str), { | ||
title: 'contains lowercase', | ||
@@ -53,3 +53,3 @@ description: 'must not contain lowercase characters', | ||
public lowerCase(): this { | ||
this.addInternalTestFunction((str) => !/[A-Z]/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => !/[A-Z]/.test(str), { | ||
title: 'contains uppercase', | ||
@@ -62,3 +62,3 @@ description: 'must not contain uppercase characters', | ||
public alphanumeric(): this { | ||
this.addInternalTestFunction((str) => /^[a-zA-Z0-9]*$/.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => /^[a-zA-Z0-9]*$/.test(str), { | ||
title: 'not alphanumeric', | ||
@@ -71,3 +71,3 @@ description: 'must only contain letters and numbers', | ||
public regex(regex: RegExp): this { | ||
this.addInternalTestFunction((str) => regex.test(str), { | ||
this.addNonRequiredInternalTestFunction((str) => regex.test(str), { | ||
title: 'failed regex', | ||
@@ -74,0 +74,0 @@ description: `failed the regular expression '${regex}'`, |
@@ -107,3 +107,12 @@ import { expect } from 'chai'; | ||
}); | ||
it('should pass', () => { | ||
const schema: Schema = new Schema() | ||
.addField('required', (f) => f | ||
.string() | ||
.addRule((r) => r.minLength(4)) | ||
.addRule((r) => r.maxLength(10)), | ||
); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "mev", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "Another validator..", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
303541
3496