Skip to content

pattern validation for optional fields #66539

@cheng93

Description

@cheng93

Which @angular/* package(s) are relevant/related to the feature request?

forms

Description

optional fields can be represented by

foo: string | null;

the pattern signature is the following and we get a ts(2345) error.

export function pattern<TPathKind extends PathKind = PathKind.Root>(
  path: SchemaPath<string, SchemaPathRules.Supported, TPathKind>,
  pattern: RegExp | LogicFn<string | undefined, RegExp | undefined, TPathKind>,
  config?: BaseValidatorConfig<string, TPathKind>,
) {

Proposed solution

export function pattern<TPathKind extends PathKind = PathKind.Root>(
  path: SchemaPath<string | null, SchemaPathRules.Supported, TPathKind>,
  pattern: RegExp | LogicFn<string | null | undefined, RegExp | undefined, TPathKind>,
  config?: BaseValidatorConfig<string | null, TPathKind>,
) {
export function pattern<TValue extends string | null, TPathKind extends PathKind = PathKind.Root>(
  path: SchemaPath<TValue , SchemaPathRules.Supported, TPathKind>,
  pattern: RegExp | LogicFn<TValue  | undefined, RegExp | undefined, TPathKind>,
  config?: BaseValidatorConfig<TValue , TPathKind>,
) {

Alternatives considered

Alternative is to just have it as string and use ''

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions