Skip to content

Impove imports #61908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/forms/experimental/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ export * from './src/api/logic';
export * from './src/api/types';
export * from './src/api/control';
export * from './src/api/metadata';
export * from './src/api/validators';
export * from './src/api/async';
export * from './src/api/data';
17 changes: 8 additions & 9 deletions packages/forms/experimental/src/api/validators/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FieldPath} from "../types";
import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validators/types';
import {FieldPath} from '../types';
import {validate} from '../logic';

import {BaseValidatorConfig} from './types';

/**
* A regular expression that matches valid e-mail addresses.
Expand Down Expand Up @@ -45,12 +44,12 @@ const EMAIL_REGEXP =
/^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

/*
* Validator validating email addresses.
*
* @param path Path to the target field
* @param maxValue The minimum value, or a LogicFn returning it.
* @param config Optional, currently allows providing custom errors function.
*/
* Validator validating email addresses.
*
* @param path Path to the target field
* @param maxValue The minimum value, or a LogicFn returning it.
* @param config Optional, currently allows providing custom errors function.
*/
export function email(path: FieldPath<string>, config?: BaseValidatorConfig<string>) {
return validate(path, (ctx) => {
if (!EMAIL_REGEXP.test(ctx.value())) {
Expand Down
15 changes: 10 additions & 5 deletions packages/forms/experimental/src/api/validators/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FieldPath, LogicFn, MAX, metadata, validate} from '@angular/forms/experimental';
import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validators/types';
import {FieldPath, LogicFn} from '../types';
import {metadata, validate} from '../logic';
import {MAX} from '../metadata';
import {BaseValidatorConfig} from './types';

/**
* Validator requiring a field value to be smaller than or equal to a maximum value.
Expand All @@ -16,9 +18,12 @@ import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validator
* @param maxValue The minimum value, or a LogicFn returning it.
* @param config Optional, currently allows providing custom errors function.
*/
export function max(path: FieldPath<number>, maxValue: (number | LogicFn<number, number>), config?: BaseValidatorConfig<number>) {
const reactiveMaxValue = (typeof maxValue === 'number') ?
() => maxValue : maxValue;
export function max(
path: FieldPath<number>,
maxValue: number | LogicFn<number, number>,
config?: BaseValidatorConfig<number>,
) {
const reactiveMaxValue = typeof maxValue === 'number' ? () => maxValue : maxValue;

metadata(path, MAX, reactiveMaxValue);
validate(path, (ctx) => {
Expand Down
14 changes: 10 additions & 4 deletions packages/forms/experimental/src/api/validators/max_length.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FieldPath, LogicFn, MAX_LENGTH, metadata, validate} from '@angular/forms/experimental';
import {BaseValidatorConfig, ValueWithLength} from '@angular/forms/experimental/src/api/validators/types';
import {FieldPath, LogicFn} from '../types';
import {metadata, validate} from '../logic';
import {BaseValidatorConfig, ValueWithLength} from './types';
import {MAX_LENGTH} from '../metadata';

/**
* Validator requiring a field value's length to be smaller than or equal to a maximum length.
Expand All @@ -16,11 +18,15 @@ import {BaseValidatorConfig, ValueWithLength} from '@angular/forms/experimental/
* @param maxLength The maximum length, or a LogicFn returning it.
* @param config Optional, currently allows providing custom errors function.
*/
export function maxLength(path: FieldPath<ValueWithLength>, maxLength: number | LogicFn<ValueWithLength, number>, config?: BaseValidatorConfig<ValueWithLength>) {
export function maxLength(
path: FieldPath<ValueWithLength>,
maxLength: number | LogicFn<ValueWithLength, number>,
config?: BaseValidatorConfig<ValueWithLength>,
) {
const reactiveMaxLengthValue = typeof maxLength === 'number' ? () => maxLength : maxLength;
metadata(path, MAX_LENGTH, reactiveMaxLengthValue);

validate(path, ctx => {
validate(path, (ctx) => {
if (ctx.value().length > reactiveMaxLengthValue(ctx)) {
if (config?.errors) {
return config.errors(ctx);
Expand Down
18 changes: 10 additions & 8 deletions packages/forms/experimental/src/api/validators/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {metadata, validate} from "../logic";
import { FieldPath, LogicFn } from "../types";
import {MIN} from '@angular/forms/experimental';
import { BaseValidatorConfig } from "./types";

import {FieldPath, LogicFn} from '../types';
import {metadata, validate} from '../logic';
import {BaseValidatorConfig} from './types';
import {MIN} from '../metadata';

/**
* Validator requiring a field value to be greater than or equal to a minimum value.
Expand All @@ -19,9 +18,12 @@ import { BaseValidatorConfig } from "./types";
* @param minValue The minimum value, or a LogicFn returning it.
* @param config Optional, currently allows providing custom errors function.
*/
export function min(path: FieldPath<number>, minValue: (number | LogicFn<number, number>), config?: BaseValidatorConfig<number>) {
const reactiveMinValue = (typeof minValue === 'number') ?
() => minValue : minValue;
export function min(
path: FieldPath<number>,
minValue: number | LogicFn<number, number>,
config?: BaseValidatorConfig<number>,
) {
const reactiveMinValue = typeof minValue === 'number' ? () => minValue : minValue;
metadata(path, MIN, reactiveMinValue);
validate(path, (ctx) => {
// TODO(kirjs): Do we need to handle Null, parseFloat, NaN?
Expand Down
14 changes: 6 additions & 8 deletions packages/forms/experimental/src/api/validators/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {metadata, validate} from '../logic';
import {REQUIRED} from '../metadata';
import {FieldPath, LogicFn} from '../types';
import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validators/types';
import {BaseValidatorConfig} from './types';

/**
* Adds logic to a field to conditionally make it required. A required field has metadata to
Expand All @@ -23,11 +23,11 @@ import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validator
* @template T The data type of the field the logic is being added to.
*/
export function required<T>(
path: FieldPath<T>,
config?: BaseValidatorConfig<T> & {
emptyPredicate?: (value: T) => boolean,
when?: NoInfer<LogicFn<T, boolean>>
}
path: FieldPath<T>,
config?: BaseValidatorConfig<T> & {
emptyPredicate?: (value: T) => boolean;
when?: NoInfer<LogicFn<T, boolean>>;
},
): void {
const emptyPredicate = config?.emptyPredicate || ((value) => value == null || value === '');
const condition = config?.when ?? (() => true);
Expand All @@ -44,5 +44,3 @@ export function required<T>(
return undefined;
});
}


Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FieldPath, metadata, REQUIRED, validate} from '@angular/forms/experimental';
import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validators/types';
import {FieldPath} from '../types';
import {metadata, validate} from '../logic';
import {BaseValidatorConfig} from './types';
import {REQUIRED} from '../metadata';

/**
* Validator requiring a field value to be true, generally is used for checkboxes that must be checked.
Expand All @@ -17,7 +19,7 @@ import {BaseValidatorConfig} from '@angular/forms/experimental/src/api/validator
export function requiredTrue(path: FieldPath<boolean>, config?: BaseValidatorConfig<boolean>) {
metadata(path, REQUIRED, () => true);

validate(path, ctx => {
validate(path, (ctx) => {
if (!ctx.value()) {
if (config?.errors) {
return config.errors(ctx);
Expand Down
4 changes: 1 addition & 3 deletions packages/forms/experimental/src/api/validators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
* found in the LICENSE file at https://angular.io/license
*/


import {LogicFn, ValidationResult} from '@angular/forms/experimental';
import {LogicFn, ValidationResult} from '../types';

// TODO(kirjs): Consider using {length: number}
export type ValueWithLength = Array<unknown> | string;


export interface BaseValidatorConfig<T> {
errors?: LogicFn<T, ValidationResult>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {form} from '../../../src/api/structure';
import {email} from '../../../src/api/validators';
import {form, email} from '../../../public_api';

describe('email validator', () => {
it('returns requiredTrue error when the value is false', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/forms/experimental/test/api/validators/max.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {MAX} from '../../../src/api/metadata';
import {form} from '../../../src/api/structure';
import {max} from '../../../src/api/validators';
import {MAX, form, max} from '../../../public_api';

describe('max validator', () => {
it('returns max error when the value is larger', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {MAX_LENGTH} from '../../../src/api/metadata';
import {form} from '../../../src/api/structure';
import {maxLength} from '../../../src/api/validators';
import {MAX_LENGTH, form, maxLength} from '../../../public_api';

describe('maxLength validator', () => {
it('returns maxLength error when the length is larger for strings', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/forms/experimental/test/api/validators/min.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {MIN} from '../../../src/api/metadata';
import {form} from '../../../src/api/structure';
import {min} from '../../../src/api/validators';
import {MIN, form, min} from '../../../public_api';

describe('min validator', () => {
it('returns min error when the value is smaller', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {MIN_LENGTH} from '../../../src/api/metadata';
import {form} from '../../../src/api/structure';
import {minLength} from '../../../src/api/validators';
import {MIN_LENGTH, form, minLength} from '../../../public_api';

describe('minLength validator', () => {
it('returns minLength error when the length is smaller for strings', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {PATTERN} from '@angular/forms/experimental';
import {pattern} from '@angular/forms/experimental/src/api/validators/pattern';
import {form} from '../../../src/api/structure';
import {PATTERN, form, pattern} from '../../../public_api';

describe('pattern validator', () => {
it('validates whether a value matches the string pattern', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {form} from '../../../src/api/structure';
import {required} from '../../../src/api/validators';
import {form, required} from '../../../public_api';

describe('required validator', () => {
it('returns required Error when the value is not present', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {form} from '../../../src/api/structure';
import {requiredTrue} from '../../../src/api/validators';
import {requiredTrue, form} from '../../../public_api';

describe('requiredTrue validator', () => {
it('returns requiredTrue error when the value is false', () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/forms/experimental/test/api/when.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

import {Injector, Signal, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {validate} from '../../src/api/logic';
import {applyEach, applyWhen, applyWhenValue, form} from '../../src/api/structure';
import {SchemaOrSchemaFn} from '../../src/api/types';
import {
validate,
SchemaOrSchemaFn,
applyEach,
applyWhen,
applyWhenValue,
form,
} from '../../public_api';

export interface User {
first: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/experimental/test/field_context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import {signal, WritableSignal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {FieldContext, FieldPath, form, validate} from '@angular/forms/experimental';
import {FieldContext, FieldPath, form, validate} from '../public_api';

function testContext<T>(
s: WritableSignal<T>,
Expand Down
48 changes: 39 additions & 9 deletions packages/forms/experimental/test/field_node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@

import {computed, Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {disabled, error, readonly, validate, validateTree} from '../src/api/logic';
import {REQUIRED} from '../src/api/metadata';
import {apply, applyEach, form, submit} from '../src/api/structure';
import {FormTreeError, SchemaOrSchemaFn} from '../src/api/types';
import {required} from '../src/api/validators';
import {
disabled,
required,
error,
readonly,
validate,
validateTree,
REQUIRED,
FormTreeError,
SchemaOrSchemaFn,
apply,
applyEach,
form,
submit,
} from '../public_api';

const noopSchema: SchemaOrSchemaFn<unknown> = () => {};

Expand Down Expand Up @@ -376,7 +386,12 @@ describe('FieldNode', () => {
);

expect(f.a().disabled()).toBe(true);
expect(f.a().disabledReasons()).toEqual([{field: f.a, reason: 'a cannot be changed'}]);
expect(f.a().disabledReasons()).toEqual([
{
field: f.a,
reason: 'a cannot be changed',
},
]);
});

it('should not have disabled reason if not disabled', () => {
Expand All @@ -394,7 +409,12 @@ describe('FieldNode', () => {
f.a().value.set(6);

expect(f.a().disabled()).toBe(true);
expect(f.a().disabledReasons()).toEqual([{field: f.a, reason: 'a cannot be changed'}]);
expect(f.a().disabledReasons()).toEqual([
{
field: f.a,
reason: 'a cannot be changed',
},
]);
});

it('disabled reason should propagate to children', () => {
Expand All @@ -407,9 +427,19 @@ describe('FieldNode', () => {
);

expect(f().disabled()).toBe(true);
expect(f().disabledReasons()).toEqual([{field: f, reason: 'form unavailable'}]);
expect(f().disabledReasons()).toEqual([
{
field: f,
reason: 'form unavailable',
},
]);
expect(f.a().disabled()).toBe(true);
expect(f.a().disabledReasons()).toEqual([{field: f, reason: 'form unavailable'}]);
expect(f.a().disabledReasons()).toEqual([
{
field: f,
reason: 'form unavailable',
},
]);
});
});

Expand Down
Loading
Loading