8000 standard schema support by mmalerba · Pull Request #61438 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

standard schema support #61438

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 5 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Leon Senft <leonsenft@users.noreply.github.com>
  • Loading branch information
mmalerba and leonsenft committed Jul 1, 2025
commit 94d44f2308e4783cd9780803b864d823330b176a
36 changes: 18 additions & 18 deletions packages/forms/experimental/src/api/standard_schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import {computed, resource} from '@angular/core';
import {computed, resource, ɵisPromise} from '@angular/core';
import {validateAsync} from './async';
import {define} from './data';
import {validateTree} from './logic';
import {StandardSchemaV1} from './standard_schema_types';
import {Field, FieldPath} from './types';
import {Field, FieldPath, FormTreeError} from './types';

/**
* A validation error produced by running a standard schema validator.
*/
interface StandardSchemaFormTreeError extends FormTreeError {
issue: StandardSchemaV1.Issue;
}

/**
* Validates a field using a `StandardSchemaV1` compatible validator (e.g. a zod validator).
*
* See https://github.com/standard-schema/standard-schema for more about standard schema.
*
* @param path The `FieldPath` to the field to validate.
* @param schema The standard schema copatible validator to use for validation.
* @param schema The standard schema compatible validator to use for validation.
* @template T The type of the field being validated.
*/
export function validateStandardSchema<T>(
Expand All @@ -28,7 +37,7 @@ export function validateStandardSchema<T>(
validateTree(path, ({state, fieldOf}) => {
// Skip sync validation if the result is a Promise.
const result = state.data(schemaResult)!();
if (isPromise(result)) {
if (ɵisPromise(result)) {
return [];
}
return result.issues?.map((issue) => standardIssueToFormTreeError(fieldOf(path), issue)) ?? [];
Expand All @@ -38,7 +47,7 @@ export function validateStandardSchema<T>(
params: ({state}) => {
// Skip async validation if the result is *not* a Promise.
const result = state.data(schemaResult)!();
return isPromise(result) ? result : undefined;
return ɵisPromise(result) ? result : undefined;
},
factory: (params) => {
return resource({
Expand All @@ -59,7 +68,10 @@ export function validateStandardSchema<T>(
* @param issue The `StandardSchemaV1.Issue` to convert.
* @returns A `FormTreeError` representing the issue.
*/
export function standardIssueToFormTreeError(field: Field<unknown>, issue: StandardSchemaV1.Issue) {
export function standardIssueToFormTreeError(
field: Field<unknown>,
issue: StandardSchemaV1.Issue,
): StandardSchemaFormTreeError {
let target = field as Field<Record<PropertyKey, unknown>>;
for (const pathPart of issue.path ?? []) {
const pathKey = typeof pathPart === 'object' ? pathPart.key : pathPart;
Expand All @@ -71,15 +83,3 @@ export function standardIssueToFormTreeError(field: Field<unknown>, issue: Stand
issue,
};
}

/**
* Checks if a value is a Promise.
* Use this function rather than `instanceof Promise` because the value could be a thenable rather
* than a proper `Promise` (e.g. from nodejs)
*
* @param value The value to check.
* @returns `true` if the value is a Promise, `false` otherwise.
*/
export function isPromise(value: Object): value is Promise<unknown> {
return typeof (value as Promise<unknown>).then === 'function';
}
3 changes: 3 additions & 0 deletions packages/forms/experimental/src/api/standard_schema_types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// DO NOT EDIT: This file is copy/pasted from https://github.com/standard-schema/standard-schema
// TODO: should we keep this copy/pasted version of depend on it from npm?

/** The Standard Schema interface. */
export interface StandardSchemaV1<Input = unknown, Output = Input> {
/** The Standard Schema properties. */
Expand Down
6 changes: 2 additions & 4 deletions packages/forms/experimental/test/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ ts_library(
name = "test_lib",
testonly = True,
srcs = glob(["**/*.ts"]),
# Visible to //:saucelabs_unit_tests_poc target
visibility = ["//:__pkg__"],
deps = [
"//packages/common/http",
"//packages/common/http/testing",
Expand All @@ -21,7 +19,7 @@ ts_library(
)

# To run this:
# yarn bazel test //packages/forms/experimental/test:test
# yarn bazel test //packages/forms/experimental/test/node:test

jasmine_node_test(
name = "test",
Expand All @@ -31,7 +29,7 @@ jasmine_node_test(
],
)

# yarn bazel test //packages/forms/experimental/test:test_web_chromium
# yarn bazel test //packages/forms/experimental/test/node:test_web_chromium
karma_web_test_suite(
name = "test_web",
tags = ["manual"],
Expand Down
4 changes: 1 addition & 3 deletions packages/forms/experimental/test/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ ts_library(
name = "test_lib",
testonly = True,
srcs = glob(["**/*.ts"]),
# Visible to //:saucelabs_unit_tests_poc target
visibility = ["//:__pkg__"],
deps = [
"//packages/core",
"//packages/core/testing",
Expand All @@ -19,7 +17,7 @@ ts_library(
)

# To run this:
# yarn bazel test //packages/forms/experimental/test:test
# yarn bazel test //packages/forms/experimental/test/web:test

karma_web_test_suite(
name = "test",
Expand Down
Loading
0