Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ValidationRules): add number validation rules #519

Merged
merged 2 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/implementation/validation-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const validationMessages: ValidationMessages = {
maxLength: `\${$displayName} cannot be longer than \${$config.length} character\${$config.length === 1 ? '' : 's'}.`,
minItems: `\${$displayName} must contain at least \${$config.count} item\${$config.count === 1 ? '' : 's'}.`,
maxItems: `\${$displayName} cannot contain more than \${$config.count} item\${$config.count === 1 ? '' : 's'}.`,
min: `\${$displayName} must be at least \${$config.constraint}.`,
max: `\${$displayName} must be at most \${$config.constraint}.`,
range: `\${$displayName} must be between or equal to \${$config.min} and \${$config.max}.`,
between: `\${$displayName} must be between but not equal to \${$config.min} and \${$config.max}.`,
equals: `\${$displayName} must be \${$config.expectedValue}.`,
};

Expand Down
78 changes: 78 additions & 0 deletions src/implementation/validation-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ export class FluentRuleCustomizer<TObject, TValue> {
return this.fluentRules.maxItems(count);
}

/**
* Applies the "min" NUMBER validation rule to the property.
* Value must be greater than or equal to the specified constraint.
* null and undefined values are considered valid.
*/
public min(value: number) {
return this.fluentRules.min(value);
}

/**
* Applies the "max" NUMBER validation rule to the property.
* Value must be less than or equal to the specified constraint.
* null and undefined values are considered valid.
*/
public max(value: number) {
return this.fluentRules.max(value);
}

/**
* Applies the "range" NUMBER validation rule to the property.
* Value must be between or equal to the specified min and max.
* null and undefined values are considered valid.
*/
public range(min: number, max: number) {
return this.fluentRules.range(min, max);
}

/**
* Applies the "between" NUMBER validation rule to the property.
* Value must be between but not equal to the specified min and max.
* null and undefined values are considered valid.
*/
public between(min: number, max: number) {
return this.fluentRules.between(min, max);
}

/**
* Applies the "equals" validation rule to the property.
* null, undefined and empty-string values are considered valid.
Expand Down Expand Up @@ -336,6 +372,48 @@ export class FluentRules<TObject, TValue> {
.withMessageKey('maxItems');
}

/**
* Applies the "min" NUMBER validation rule to the property.
* Value must be greater than or equal to the specified constraint.
* null and undefined values are considered valid.
*/
public min(constraint: number) {
return this.satisfies((value: any) => value === null || value === undefined || value >= constraint, { constraint })
.withMessageKey('min');
}

/**
* Applies the "max" NUMBER validation rule to the property.
* Value must be less than or equal to the specified constraint.
* null and undefined values are considered valid.
*/
public max(constraint: number) {
return this.satisfies((value: any) => value === null || value === undefined || value <= constraint, { constraint })
.withMessageKey('max');
}

/**
* Applies the "range" NUMBER validation rule to the property.
* Value must be between or equal to the specified min and max.
* null and undefined values are considered valid.
*/
public range(min: number, max: number) {
return this.satisfies((value: any) => value === null || value === undefined || (value >= min && value <= max),
{ min, max })
.withMessageKey('range');
}

/**
* Applies the "between" NUMBER validation rule to the property.
* Value must be between but not equal to the specified min and max.
* null and undefined values are considered valid.
*/
public between(min: number, max: number) {
return this.satisfies((value: any) => value === null || value === undefined || (value > min && value < max),
{ min, max })
.withMessageKey('between');
}

/**
* Applies the "equals" validation rule to the property.
* null and undefined values are considered valid.
Expand Down
77 changes: 77 additions & 0 deletions test/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,83 @@ describe('Validator', () => {
.then(done);
});

it('validates numeric properties', (done: () => void) => {
const obj = { value: 1 };
let rules = ValidationRules.ensure('value').min(1).rules;
validator.validateObject(obj, rules)
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', true, null)];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').max(1).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', true, null)];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').range(0, 1).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', true, null)];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').between(0, 2).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', true, null)];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').min(2).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', false, 'Value must be at least 2.')];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').max(0).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', false, 'Value must be at most 0.')];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').range(2, 3).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', false,
'Value must be between or equal to 2 and 3.')];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(() => {
rules = ValidationRules.ensure('value').between(1, 3).rules;
return validator.validateObject(obj, rules);
})
.then(results => {
const expected = [new ValidateResult(rules[0][0], obj, 'value', false,
'Value must be between but not equal to 1 and 3.')];
expected[0].id = results[0].id;
expect(results).toEqual(expected);
})
.then(done);
});

it('handles numeric properties', (done: () => void) => {
const objStr = {} as any;
objStr['2'] = 'test';
Expand Down