MCPcopy
hub / github.com/angular/angular / minDate

Function minDate

packages/forms/signals/src/api/rules/validation/min_date.ts:32–72  ·  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

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

Callers 2

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…