MCPcopy
hub / github.com/angular/angular / pattern

Function pattern

packages/forms/signals/src/api/rules/validation/pattern.ts:31–60  ·  view source on GitHub ↗
(
  path: SchemaPath<string, SchemaPathRules.Supported, TPathKind>,
  pattern: RegExp | LogicFn<string | undefined, RegExp | undefined, TPathKind>,
  config?: BaseValidatorConfig<string, TPathKind>,
)

Source from the content-addressed store, hash-verified

29 * @publicApi 22.0
30 */
31export function pattern<TPathKind extends PathKind = PathKind.Root>(
32 path: SchemaPath<string, SchemaPathRules.Supported, TPathKind>,
33 pattern: RegExp | LogicFn<string | undefined, RegExp | undefined, TPathKind>,
34 config?: BaseValidatorConfig<string, TPathKind>,
35) {
36 const PATTERN_MEMO = metadata(path, createMetadataKey<RegExp | undefined>(), (ctx) => {
37 if (config?.when && !config.when(ctx)) {
38 return undefined;
39 }
40 return pattern instanceof RegExp ? pattern : pattern(ctx);
41 });
42 metadata(path, PATTERN, ({state}) => state.metadata(PATTERN_MEMO)!());
43 validate(path, (ctx) => {
44 if (isEmpty(ctx.value())) {
45 return undefined;
46 }
47 const pattern = ctx.state.metadata(PATTERN_MEMO)!();
48 if (pattern === undefined) {
49 return undefined;
50 }
51 if (!pattern.test(ctx.value())) {
52 if (config?.error) {
53 return getOption(config.error, ctx);
54 } else {
55 return patternError(pattern, {message: getOption(config?.message, ctx)});
56 }
57 }
58 return undefined;
59 });
60}

Callers 3

TestCmpClass · 0.90
TestCmpClass · 0.85
pattern.spec.tsFile · 0.85

Calls 10

metadataFunction · 0.90
createMetadataKeyFunction · 0.90
validateFunction · 0.90
isEmptyFunction · 0.90
getOptionFunction · 0.90
patternErrorFunction · 0.90
whenMethod · 0.80
metadataMethod · 0.65
valueMethod · 0.65
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…