MCPcopy Create free account
hub / github.com/angular/angular / minDate

Function minDate

packages/forms/signals/src/api/rules/validation/min_date.ts:37–77  ·  view source on GitHub ↗
(
  path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
  minDateValue: Date | LogicFn<TValue, Date | undefined, TPathKind>,
  config?: BaseValidatorConfig<TValue, TPathKind>,
)

Source from the content-addressed store, hash-verified

35 * @publicApi 22.0
36 */
37export function minDate<TValue extends Date | null, TPathKind extends PathKind = PathKind.Root>(
38 path: SchemaPath<TValue, SchemaPathRules.Supported, TPathKind>,
39 minDateValue: Date | LogicFn<TValue, Date | undefined, TPathKind>,
40 config?: BaseValidatorConfig<TValue, TPathKind>,
41): void {
42 const MIN_MEMO = createMetadataKey<Date | undefined>();
43
44 // Memoize the minimum valid date.
45 metadata(path, MIN_MEMO, (ctx) => {
46 if (config?.when && !config.when(ctx)) {
47 return undefined;
48 }
49 return typeof minDateValue === 'function' ? minDateValue(ctx) : minDateValue;
50 });
51
52 // Publish the memoized minimum date for aggregation.
53 metadata(path, MIN_DATE, ({state}) => state.metadata(MIN_MEMO)!());
54
55 // Use `MIN_DATE` to define the `min` property of the field.
56 metadata(path, MIN, () => MIN_DATE as LimitKey<TValue>);
57
58 // Validate that the field value is not less than the minimum date.
59 validate(path, (ctx) => {
60 const value = ctx.value();
61 if (value === null || Number.isNaN(value.getTime())) {
62 return undefined;
63 }
64 const min = ctx.state.metadata(MIN_MEMO)!();
65 if (min === undefined || Number.isNaN(min.getTime())) {
66 return undefined;
67 }
68 if (value < min) {
69 if (config?.error) {
70 return getOption(config.error, ctx);
71 } else {
72 return minDateError(min, {message: getOption(config?.message, ctx)});
73 }
74 }
75 return undefined;
76 });
77}

Callers 3

TestCmpClass · 0.90
TestCmpClass · 0.85
min_date.spec.tsFile · 0.85

Calls 8

createMetadataKeyFunction · 0.90
metadataFunction · 0.90
validateFunction · 0.90
getOptionFunction · 0.90
minDateErrorFunction · 0.90
whenMethod · 0.80
metadataMethod · 0.65
valueMethod · 0.65

Tested by

no test coverage detected