(
path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
config?: BaseValidatorConfig<TValue, TPathKind> & {
when?: NoInfer<LogicFn<TValue, boolean, TPathKind>>;
},
)
| 31 | * @publicApi 22.0 |
| 32 | */ |
| 33 | export function required<TValue, TPathKind extends PathKind = PathKind.Root>( |
| 34 | path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>, |
| 35 | config?: BaseValidatorConfig<TValue, TPathKind> & { |
| 36 | when?: NoInfer<LogicFn<TValue, boolean, TPathKind>>; |
| 37 | }, |
| 38 | ): void { |
| 39 | const REQUIRED_MEMO = metadata(path, createMetadataKey<boolean>(), (ctx) => |
| 40 | config?.when ? config.when(ctx) : true, |
| 41 | ); |
| 42 | metadata(path, REQUIRED, ({state}) => state.metadata(REQUIRED_MEMO)!()!); |
| 43 | validate(path, (ctx) => { |
| 44 | if (ctx.state.metadata(REQUIRED_MEMO)!() && isEmpty(ctx.value())) { |
| 45 | if (config?.error) { |
| 46 | return getOption(config.error, ctx); |
| 47 | } else { |
| 48 | return requiredError({message: getOption(config?.message, ctx)}); |
| 49 | } |
| 50 | } |
| 51 | return undefined; |
| 52 | }); |
| 53 | } |
searching dependent graphs…