-
Notifications
You must be signed in to change notification settings - Fork 111
/
required-true.ts
44 lines (41 loc) · 1.13 KB
/
required-true.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { ValidationErrors } from 'ngrx-forms';
/**
* A validation function that requires the value to be `true`. Considers `null` and
* `undefined` as valid. Combine this function with the `required` validation
* function if `null` or `undefined` should be considered invalid.
*
* The validation error returned by this validation function has the following shape:
*
* ```typescript
* {
* required: {
* actual: boolean;
* };
* }
* ```
*
* Usually you would use this validation function in conjunction with the `validate`
* update function to perform synchronous validation in your reducer:
*
* ```typescript
* updateGroup<MyFormValue>({
* agreeWithTermsOfService: validate(requiredTrue),
* })
* ```
*
* Note that this function is generic to allow the compiler to properly infer the type
* of the `validate` function for both optional and non-optional controls.
*/
export function requiredTrue<T extends boolean | null | undefined>(value: T): ValidationErrors {
if (value === null || value === undefined) {
return {};
}
if (value) {
return {};
}
return {
required: {
actual: value,
},
};
}