Skip to content

Commit 447b6e9

Browse files
authored
refactor(forms): move addDefaultField out of public API (#63389)
1 parent f90ca1c commit 447b6e9

File tree

7 files changed

+37
-46
lines changed

7 files changed

+37
-46
lines changed

goldens/public-api/forms/signals/index.api.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ import { ValidationErrors } from '@angular/forms';
2424
import { ValidatorFn } from '@angular/forms';
2525
import { WritableSignal } from '@angular/core';
2626

27-
// @public
28-
export function addDefaultField<E extends ValidationError>(error: WithOptionalField<E>, field: Field<unknown>): E;
29-
30-
// @public (undocumented)
31-
export function addDefaultField<E extends ValidationError>(errors: TreeValidationResult<E>, field: Field<unknown>): ValidationResult<E>;
32-
3327
// @public
3428
export class AggregateProperty<TAcc, TItem> {
3529
// (undocumented)

packages/forms/signals/src/api/async.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import {httpResource, HttpResourceOptions, HttpResourceRequest} from '@angular/common/http';
1010
import {computed, ResourceRef, Signal} from '@angular/core';
1111
import {FieldNode} from '../field/node';
12+
import {addDefaultField} from '../field/validation';
1213
import {FieldPathNode} from '../schema/path_node';
1314
import {assertPathIsCurrent} from '../schema/schema';
1415
import {property} from './logic';
1516
import {FieldContext, FieldPath, PathKind, TreeValidationResult} from './types';
16-
import {addDefaultField} from './validation_errors';
1717

1818
/**
1919
* A function that takes the result of an async operation and the current field context, and maps it

packages/forms/signals/src/api/logic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {addDefaultField} from '../field/validation';
910
import {FieldPathNode} from '../schema/path_node';
1011
import {assertPathIsCurrent} from '../schema/schema';
1112
import {AggregateProperty, createProperty, Property} from './property';
@@ -17,7 +18,6 @@ import type {
1718
PathKind,
1819
TreeValidator,
1920
} from './types';
20-
import {addDefaultField} from './validation_errors';
2121

2222
/**
2323
* Adds logic to a field to conditionally disable it. A disabled field does not contribute to the

packages/forms/signals/src/api/structure.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {inject, Injector, runInInjectionContext, WritableSignal} from '@angular/
1111
import {BasicFieldAdapter, FieldAdapter} from '../field/field_adapter';
1212
import {FormFieldManager} from '../field/manager';
1313
import {FieldNode} from '../field/node';
14+
import {addDefaultField} from '../field/validation';
1415
import {FieldPathNode} from '../schema/path_node';
1516
import {assertPathIsCurrent, isSchemaOrSchemaFn, SchemaImpl} from '../schema/schema';
1617
import {isArray} from '../util/type_guards';
@@ -25,7 +26,7 @@ import type {
2526
SchemaOrSchemaFn,
2627
TreeValidationResult,
2728
} from './types';
28-
import {addDefaultField, ValidationError, WithOptionalField} from './validation_errors';
29+
import {ValidationError, WithOptionalField} from './validation_errors';
2930

3031
/** Options that may be specified when creating a form. */
3132
export interface FormOptions {

packages/forms/signals/src/api/validation_errors.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,11 @@
77
*/
88

99
import type {StandardSchemaV1} from '@standard-schema/spec';
10-
import {isArray} from '../util/type_guards';
11-
import {Field, Mutable, TreeValidationResult, ValidationResult} from './types';
10+
import {Field} from './types';
1211

1312
/** Internal symbol used for class branding. */
1413
const BRAND = Symbol();
1514

16-
/**
17-
* Sets the given field on the given error(s) if it does not already have a field.
18-
* @param errors The error(s) to add the field to
19-
* @param field The default field to add
20-
* @returns The passed in error(s), with its field set.
21-
*/
22-
export function addDefaultField<E extends ValidationError>(
23-
error: WithOptionalField<E>,
24-
field: Field<unknown>,
25-
): E;
26-
export function addDefaultField<E extends ValidationError>(
27-
errors: TreeValidationResult<E>,
28-
field: Field<unknown>,
29-
): ValidationResult<E>;
30-
export function addDefaultField<E extends ValidationError>(
31-
errors: TreeValidationResult<E>,
32-
field: Field<unknown>,
33-
): ValidationResult<E> {
34-
if (isArray(errors)) {
35-
for (const error of errors) {
36-
(error as Mutable<ValidationError>).field ??= field;
37-
}
38-
} else if (errors) {
39-
(errors as Mutable<ValidationError>).field ??= field;
40-
}
41-
return errors as ValidationResult<E>;
42-
}
43-
4415
/**
4516
* Options used to create a `ValidationError`.
4617
*/

packages/forms/signals/src/api/validators/standard_schema.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88

99
import {computed, resource, ɵisPromise} from '@angular/core';
1010
import type {StandardSchemaV1} from '@standard-schema/spec';
11+
import {addDefaultField} from '../../field/validation';
1112
import {validateAsync} from '../async';
1213
import {property, validateTree} from '../logic';
1314
import {Field, FieldPath} from '../types';
14-
import {
15-
addDefaultField,
16-
standardSchemaError,
17-
StandardSchemaValidationError,
18-
} from '../validation_errors';
15+
import {standardSchemaError, StandardSchemaValidationError} from '../validation_errors';
1916

2017
/**
2118
* Utility type that removes a string index key when its value is `unknown`,

packages/forms/signals/src/field/validation.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import {computed, Signal} from '@angular/core';
10-
import type {ValidationResult} from '../api/types';
11-
import type {ValidationError} from '../api/validation_errors';
10+
import type {Field, Mutable, TreeValidationResult, ValidationResult} from '../api/types';
11+
import type {ValidationError, WithOptionalField} from '../api/validation_errors';
1212
import {isArray} from '../util/type_guards';
1313
import type {FieldNode} from './node';
1414
import {reduceChildren, shortCircuitFalse} from './util';
@@ -350,3 +350,31 @@ function normalizeErrors(error: ValidationResult): readonly ValidationError[] {
350350

351351
return [error as ValidationError];
352352
}
353+
354+
/**
355+
* Sets the given field on the given error(s) if it does not already have a field.
356+
* @param errors The error(s) to add the field to
357+
* @param field The default field to add
358+
* @returns The passed in error(s), with its field set.
359+
*/
360+
export function addDefaultField<E extends ValidationError>(
361+
error: WithOptionalField<E>,
362+
field: Field<unknown>,
363+
): E;
364+
export function addDefaultField<E extends ValidationError>(
365+
errors: TreeValidationResult<E>,
366+
field: Field<unknown>,
367+
): ValidationResult<E>;
368+
export function addDefaultField<E extends ValidationError>(
369+
errors: TreeValidationResult<E>,
370+
field: Field<unknown>,
371+
): ValidationResult<E> {
372+
if (isArray(errors)) {
373+
for (const error of errors) {
374+
(error as Mutable<ValidationError>).field ??= field;
375+
}
376+
} else if (errors) {
377+
(errors as Mutable<ValidationError>).field ??= field;
378+
}
379+
return errors as ValidationResult<E>;
380+
}

0 commit comments

Comments
 (0)