( path: SchemaPath<string, SchemaPathRules.Supported, TPathKind>, pattern: RegExp | LogicFn<string | undefined, RegExp | undefined, TPathKind>, config?: BaseValidatorConfig<string, TPathKind>, )
| 29 | * @publicApi 22.0 |
| 30 | */ |
| 31 | export 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 | } |
no test coverage detected
searching dependent graphs…