5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
+
8
9
import { computed } from '@angular/core' ;
9
10
import { aggregateProperty , property , validate } from '../logic' ;
10
11
import { PATTERN } from '../property' ;
@@ -13,11 +14,16 @@ import {ValidationError} from '../validation_errors';
13
14
import { BaseValidatorConfig } from './util' ;
14
15
15
16
/**
16
- * Validator allowing to validate a string against a pattern.
17
+ * Binds a validator to the given path that requires the value to match a specific regex pattern.
18
+ * This function can only be called on string paths.
19
+ * In addition to binding a validator, this function adds `PATTERN` property to the field.
17
20
*
18
- * @param path Path to the target field
19
- * @param pattern Regular expression to validate against.
20
- * @param config Optional, currently allows providing custom errors function.
21
+ * @param path Path of the field to validate
22
+ * @param pattern The RegExp pattern to match, or a LogicFn that returns the RegExp pattern.
23
+ * @param config Optional, allows providing any of the following options:
24
+ * - `error`: Custom validation error(s) to be used instead of the default `ValidationError.pattern(pattern)`
25
+ * or a function that receives the `FieldContext` and returns custom validation error(s).
26
+ * @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
21
27
*/
22
28
export function pattern < TPathKind extends PathKind = PathKind . Root > (
23
29
path : FieldPath < string , TPathKind > ,
@@ -31,7 +37,8 @@ export function pattern<TPathKind extends PathKind = PathKind.Root>(
31
37
validate ( path , ( ctx ) => {
32
38
const pattern = ctx . state . property ( PATTERN_MEMO ) ! ( ) ;
33
39
34
- // A pattern validator should not fail on an empty value.
40
+ // A pattern validator should not fail on an empty value. This matches the behavior of HTML's
41
+ // built in `pattern` attribute.
35
42
if ( pattern === undefined || ctx . value ( ) == null || ctx . value ( ) === '' ) {
36
43
return undefined ;
37
44
}
0 commit comments