( path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>, minLength: number | LogicFn<TValue, number | undefined, TPathKind>, config?: BaseValidatorConfig<TValue, TPathKind>, )
| 37 | * @publicApi 22.0 |
| 38 | */ |
| 39 | export function minLength< |
| 40 | TValue extends ValueWithLengthOrSize, |
| 41 | TPathKind extends PathKind = PathKind.Root, |
| 42 | >( |
| 43 | path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>, |
| 44 | minLength: number | LogicFn<TValue, number | undefined, TPathKind>, |
| 45 | config?: BaseValidatorConfig<TValue, TPathKind>, |
| 46 | ) { |
| 47 | const MIN_LENGTH_MEMO = metadata(path, createMetadataKey<number | undefined>(), (ctx) => { |
| 48 | if (config?.when && !config.when(ctx)) { |
| 49 | return undefined; |
| 50 | } |
| 51 | return typeof minLength === 'number' ? minLength : minLength(ctx); |
| 52 | }); |
| 53 | metadata(path, MIN_LENGTH, ({state}) => state.metadata(MIN_LENGTH_MEMO)!()); |
| 54 | validate(path, (ctx) => { |
| 55 | if (isEmpty(ctx.value())) { |
| 56 | return undefined; |
| 57 | } |
| 58 | const minLength = ctx.state.metadata(MIN_LENGTH_MEMO)!(); |
| 59 | if (minLength === undefined) { |
| 60 | return undefined; |
| 61 | } |
| 62 | if (getLengthOrSize(ctx.value()) < minLength) { |
| 63 | if (config?.error) { |
| 64 | return getOption(config.error, ctx); |
| 65 | } else { |
| 66 | return minLengthError(minLength, {message: getOption(config?.message, ctx)}); |
| 67 | } |
| 68 | } |
| 69 | return undefined; |
| 70 | }); |
| 71 | } |
no test coverage detected
searching dependent graphs…