Skip to content

Commit

Permalink
Merge pull request #40 from icepeng/array-validation
Browse files Browse the repository at this point in the history
feat: support array validation in `minLength`, `maxLength` and `required` validation functions
  • Loading branch information
MrWolfZ authored Feb 2, 2018
2 parents 012c24f + 3fd1d98 commit c88353a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/VALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ As mentioned in the section about [updating the state](UPDATING_THE_STATE.md) th

|Function|Description|
|-|-|
|`required`|Requires the value to be non-empty (i.e. non-`null`, non-empty `string` etc.)|
|`required`|Requires the value to be non-empty (i.e. non-`null`, non-empty `string`, non-empty `array` etc.)|
|`requiredTrue`|Requires the `boolean` value to be `true`|
|`requiredFalse`|Requires the `boolean` value to be `false`|
|`equalTo`|Requires the value to be equal to another value|
|`lessThan`|Requires the `number` value to be less than another number|
|`lessThanOrEqualTo`|Requires the `number` value to be less than or equal to another number|
|`greaterThan`|Requires the `number` value to be greater than another number|
|`greaterThanOrEqualTo`|Requires the `number` value to be greater than or equal to another number|
|`minLength`|Requires a `string` value to have a minimum length|
|`maxLength`|Requires a `string` value to have a maximum length|
|`minLength`|Requires a `string` or `array` value to have a minimum length|
|`maxLength`|Requires a `string` or `array` value to have a maximum length|
|`email`|Requires a `string` value to be a valid e-mail address|
|`pattern`|Requires a `string` value to match a regular expression|

Expand Down
18 changes: 15 additions & 3 deletions validation/src/max-length.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ describe(maxLength.name, () => {
expect(maxLength(2)(null)).toEqual({});
});

it('should return an error if value\'s length is greater than maxLength', () => {
it('should return an error if string value\'s length is greater than maxLength', () => {
expect(maxLength(2)('abc')).not.toEqual({});
});

it('should not return an error if value\'s length is equal to maxLength', () => {
it('should not return an error if string value\'s length is equal to maxLength', () => {
expect(maxLength(2)('ab')).toEqual({});
});

it('should not return an error if value\'s length is less than maxLength', () => {
it('should not return an error if string value\'s length is less than maxLength', () => {
expect(maxLength(2)('a')).toEqual({});
});

it('should return an error if array value\'s length is greater than maxLength', () => {
expect(maxLength(2)(['a', 'b', 'c'])).not.toEqual({});
});

it('should not return an error if array value\'s length is equal to maxLength', () => {
expect(maxLength(2)(['a', 'b'])).toEqual({});
});

it('should not return an error if array value\'s length is less than maxLength', () => {
expect(maxLength(2)(['a'])).toEqual({});
});

it('should return errors with maxLength, value, and actualLength properties', () => {
const maxLengthParam = 2;
const value = 'abc';
Expand Down
4 changes: 2 additions & 2 deletions validation/src/max-length.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValidationErrors } from 'ngrx-forms';

/**
* A validation function that requires a `string` value to have a maximum length.
* A validation function that requires a `string` or `array` value to have a maximum length.
* Considers `null` as valid. Combine this function with the `required` validation
* function if `null` should be considered invalid.
*
Expand Down Expand Up @@ -30,7 +30,7 @@ export function maxLength(maxLength: number) {
throw new Error(`The maxLength Validation function requires the maxLength parameter to be a non-null number, got ${maxLength}!`);
}

return (value: string | null): ValidationErrors => {
return (value: string | any[] | null): ValidationErrors => {
if (value === null) {
return {};
}
Expand Down
18 changes: 15 additions & 3 deletions validation/src/min-length.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ describe(minLength.name, () => {
expect(minLength(2)(null)).toEqual({});
});

it('should not return an error if value\'s length is greater than minLength', () => {
it('should not return an error if string value\'s length is greater than minLength', () => {
expect(minLength(2)('abc')).toEqual({});
});

it('should not return an error if value\'s length is equal to minLength', () => {
it('should not return an error if string value\'s length is equal to minLength', () => {
expect(minLength(2)('ab')).toEqual({});
});

it('should return an error if value\'s length is less than minLength', () => {
it('should return an error if string value\'s length is less than minLength', () => {
expect(minLength(2)('a')).not.toEqual({});
});

it('should not return an error if array value\'s length is greater than minLength', () => {
expect(minLength(2)(['a', 'b', 'c'])).toEqual({});
});

it('should not return an error if array value\'s length is equal to minLength', () => {
expect(minLength(2)(['a', 'b'])).toEqual({});
});

it('should return an error if array value\'s length is less than minLength', () => {
expect(minLength(2)(['a'])).not.toEqual({});
});

it('should return errors with minLength, value and actualLength properties', () => {
const minLengthValue = 2;
const value = 'a';
Expand Down
4 changes: 2 additions & 2 deletions validation/src/min-length.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValidationErrors } from 'ngrx-forms';

/**
* A validation function that requires a `string` value to have a minimum length.
* A validation function that requires a `string` or `array` value to have a minimum length.
* Considers `null` as valid. Combine this function with the `required` validation
* function if `null` should be considered invalid.
*
Expand Down Expand Up @@ -30,7 +30,7 @@ export function minLength(minLength: number) {
throw new Error(`The minLength Validation function requires the minLength parameter to be a non-null number, got ${minLength}!`);
}

return (value: string | null): ValidationErrors => {
return (value: string | any[] | null): ValidationErrors => {
if (value === null) {
return {};
}
Expand Down
13 changes: 13 additions & 0 deletions validation/src/required.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ describe(required.name, () => {
});
});

it('should return an error for empty array', () => {
const value: any[] = [];
expect(required(value)).toEqual({
required: {
actual: value,
},
});
});

it('should not return an error for number zero', () => {
expect(required(0)).toEqual({});
});
Expand All @@ -38,4 +47,8 @@ describe(required.name, () => {
it('should not return an error for false', () => {
expect(required(false)).toEqual({});
});

it('should not return an error for non-empty array', () => {
expect(required(['a'])).toEqual({});
});
});

0 comments on commit c88353a

Please sign in to comment.