From 0a05050896d5818a7eaf933618e323db69a91d36 Mon Sep 17 00:00:00 2001 From: Ha Date: Thu, 1 Feb 2018 09:33:34 +0900 Subject: [PATCH 1/3] feat: support array validation --- validation/src/max-length.spec.ts | 18 +++++++++++++++--- validation/src/max-length.ts | 2 +- validation/src/min-length.spec.ts | 18 +++++++++++++++--- validation/src/min-length.ts | 2 +- validation/src/required.spec.ts | 13 +++++++++++++ 5 files changed, 45 insertions(+), 8 deletions(-) diff --git a/validation/src/max-length.spec.ts b/validation/src/max-length.spec.ts index 6d729ebc..176df580 100644 --- a/validation/src/max-length.spec.ts +++ b/validation/src/max-length.spec.ts @@ -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'; diff --git a/validation/src/max-length.ts b/validation/src/max-length.ts index a8d7b41d..dd362d39 100644 --- a/validation/src/max-length.ts +++ b/validation/src/max-length.ts @@ -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 {}; } diff --git a/validation/src/min-length.spec.ts b/validation/src/min-length.spec.ts index 3b9e40f8..fd7a597b 100644 --- a/validation/src/min-length.spec.ts +++ b/validation/src/min-length.spec.ts @@ -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'; diff --git a/validation/src/min-length.ts b/validation/src/min-length.ts index dae766df..2317bcc5 100644 --- a/validation/src/min-length.ts +++ b/validation/src/min-length.ts @@ -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 {}; } diff --git a/validation/src/required.spec.ts b/validation/src/required.spec.ts index ee464548..02d91528 100644 --- a/validation/src/required.spec.ts +++ b/validation/src/required.spec.ts @@ -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({}); }); @@ -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({}); + }); }); From 5dc5b01897f3c952b2899422ccdf19ff2654229b Mon Sep 17 00:00:00 2001 From: Ha Date: Fri, 2 Feb 2018 16:43:50 +0900 Subject: [PATCH 2/3] docs: update description about array-supporting functions --- docs/VALIDATION.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index 4c96b84a..ae05a1c8 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -4,7 +4,7 @@ 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| @@ -12,8 +12,8 @@ As mentioned in the section about [updating the state](UPDATING_THE_STATE.md) th |`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| From 3fd1d98ba29bb7446cd0077dba8d6cf4269e279e Mon Sep 17 00:00:00 2001 From: Ha Date: Fri, 2 Feb 2018 17:10:27 +0900 Subject: [PATCH 3/3] docs: update inline description about array-supporting functions --- validation/src/max-length.ts | 2 +- validation/src/min-length.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/validation/src/max-length.ts b/validation/src/max-length.ts index dd362d39..d9572849 100644 --- a/validation/src/max-length.ts +++ b/validation/src/max-length.ts @@ -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. * diff --git a/validation/src/min-length.ts b/validation/src/min-length.ts index 2317bcc5..9cdc8ed2 100644 --- a/validation/src/min-length.ts +++ b/validation/src/min-length.ts @@ -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. *