Skip to content

Commit

Permalink
feat(validation): improve typing of errors property on form states …
Browse files Browse the repository at this point in the history
…by using module augmentation inside of validation module to add well defined error properties to `ValidationErrors` interface
  • Loading branch information
MrWolfZ committed Apr 24, 2018
1 parent 7040673 commit e202e65
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This release requires TypeScript >=2.8.0 for the conditional type support.
* add `notEqualTo` validation function ([851a1ed](https://github.com/MrWolfZ/ngrx-forms/commit/851a1ed))
* enhance all form state reducers to match type signature for `ActionReducer` (they will still throw an error if the state is `undefined`) ([f3b5fea](https://github.com/MrWolfZ/ngrx-forms/commit/f3b5fea))
* add support for `undefined` values for all validation functions ([6cce8d0](https://github.com/MrWolfZ/ngrx-forms/commit/6cce8d0), thanks @romankhrystynych for his contribution in [#65](https://github.com/MrWolfZ/ngrx-forms/pull/65)), closes [#64](https://github.com/MrWolfZ/ngrx-forms/issues/64)
* improve typing of `errors` property on form states by using module augmentation inside of validation module to add well defined error properties to `ValidationErrors` interface

#### Bugfixes

Expand Down
26 changes: 13 additions & 13 deletions validation/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
* MIT license.
* https://github.com/MrWolfZ/ngrx-forms
*/
export { email } from './src/email';
export { equalTo } from './src/equal-to';
export { greaterThan } from './src/greater-than';
export { greaterThanOrEqualTo } from './src/greater-than-or-equal-to';
export { lessThan } from './src/less-than';
export { lessThanOrEqualTo } from './src/less-than-or-equal-to';
export { maxLength } from './src/max-length';
export { minLength } from './src/min-length';
export { notEqualTo } from './src/not-equal-to';
export { pattern } from './src/pattern';
export { required } from './src/required';
export { requiredFalse } from './src/required-false';
export { requiredTrue } from './src/required-true';
export { email, EmailValidationError } from './src/email';
export { equalTo, EqualToValidationError } from './src/equal-to';
export { greaterThan, GreaterThanValidationError } from './src/greater-than';
export { greaterThanOrEqualTo, GreaterThanOrEqualToValidationError } from './src/greater-than-or-equal-to';
export { lessThan, LessThanValidationError } from './src/less-than';
export { lessThanOrEqualTo, LessThanOrEqualToValidationError } from './src/less-than-or-equal-to';
export { maxLength, MaxLengthValidationError } from './src/max-length';
export { minLength, MinLengthValidationError } from './src/min-length';
export { notEqualTo, NotEqualToValidationError } from './src/not-equal-to';
export { pattern, PatternValidationError } from './src/pattern';
export { required, RequiredValidationError } from './src/required';
export { requiredFalse, RequiredFalseValidationError } from './src/required-false';
export { requiredTrue, RequiredTrueValidationError } from './src/required-true';
12 changes: 12 additions & 0 deletions validation/src/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { ValidationErrors } from 'ngrx-forms';
// tslint:disable-next-line:max-line-length
export const NGRX_FORMS_EMAIL_VALIDATION_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/;

export interface EmailValidationError {
pattern: string;
actual: string;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
email?: EmailValidationError;
}
}

/**
* A validation function that requires a value to be a valid e-mail address.
* Considers `null`, `undefined`, and `''` as valid. Combine this function with the
Expand Down
12 changes: 12 additions & 0 deletions validation/src/equal-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface EqualToValidationError<T> {
comparand: T;
actual: T;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
equalTo?: EqualToValidationError<any>;
}
}

/**
* A validation function that requires the value to be strictly equal (i.e. `===`)
* to another value.
Expand Down
12 changes: 12 additions & 0 deletions validation/src/greater-than-or-equal-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface GreaterThanOrEqualToValidationError {
comparand: number;
actual: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
greaterThanOrEqualTo?: GreaterThanOrEqualToValidationError;
}
}

/**
* A validation function that requires the value to be greater than or equal to a number.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
12 changes: 12 additions & 0 deletions validation/src/greater-than.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface GreaterThanValidationError {
comparand: number;
actual: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
greaterThan?: GreaterThanValidationError;
}
}

/**
* A validation function that requires the value to be greater than a number.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
12 changes: 12 additions & 0 deletions validation/src/less-than-or-equal-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface LessThanOrEqualToValidationError {
comparand: number;
actual: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
lessThanOrEqualTo?: LessThanOrEqualToValidationError;
}
}

/**
* A validation function that requires the value to be less than or equal to a number.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
12 changes: 12 additions & 0 deletions validation/src/less-than.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface LessThanValidationError {
comparand: number;
actual: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
lessThan?: LessThanValidationError;
}
}

/**
* A validation function that requires the value to be less than a number.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
13 changes: 13 additions & 0 deletions validation/src/max-length.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { ValidationErrors } from 'ngrx-forms';

export interface MaxLengthValidationError {
maxLength: number;
value: string;
actualLength: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
maxLength?: MaxLengthValidationError;
}
}

/**
* A validation function that requires a `string` or `array` value to have a maximum length.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
13 changes: 13 additions & 0 deletions validation/src/min-length.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { ValidationErrors } from 'ngrx-forms';

export interface MinLengthValidationError {
minLength: number;
value: string;
actualLength: number;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
minLength?: MinLengthValidationError;
}
}

/**
* A validation function that requires a `string` or `array` value to have a minimum length.
* Considers `null` and `undefined` as valid. Combine this function with the `required`
Expand Down
12 changes: 12 additions & 0 deletions validation/src/not-equal-to.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface NotEqualToValidationError<T> {
comparand: T;
actual: T;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
notEqualTo?: NotEqualToValidationError<any>;
}
}

/**
* A validation function that requires the value to be strictly not equal (i.e. `!==`)
* to another value.
Expand Down
12 changes: 12 additions & 0 deletions validation/src/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { ValidationErrors } from 'ngrx-forms';

export interface PatternValidationError {
pattern: string;
actual: string;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
pattern?: PatternValidationError;
}
}

/**
* A validation function that requires a value to match a regex.
* Considers `null`, `undefined`, and `''` as valid. Combine this function with the
Expand Down
11 changes: 11 additions & 0 deletions validation/src/required-false.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { ValidationErrors } from 'ngrx-forms';

export interface RequiredFalseValidationError {
actual: boolean;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
required?: RequiredFalseValidationError;
}
}

/**
* A validation function that requires the value to be `false`. Considers `null` and
* `undefined` as valid. Combine this function with the `required` validation
Expand Down
11 changes: 11 additions & 0 deletions validation/src/required-true.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { ValidationErrors } from 'ngrx-forms';

export interface RequiredTrueValidationError {
actual: boolean;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
required?: RequiredTrueValidationError;
}
}

/**
* A validation function that requires the value to be `true`. Considers `null` and
* `undefined` as valid. Combine this function with the `required` validation
Expand Down
11 changes: 11 additions & 0 deletions validation/src/required.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { ValidationErrors } from 'ngrx-forms';

export interface RequiredValidationError<T> {
actual: T | null | undefined;
}

// @ts-ignore
declare module 'ngrx-forms/src/state' {
export interface ValidationErrors {
required?: RequiredValidationError<any>;
}
}

/**
* A validation function that requires the value to be non-`undefined`, non-'null',
* and non-empty.
Expand Down

0 comments on commit e202e65

Please sign in to comment.