Skip to content

Commit 193aa33

Browse files
authored
docs(forms): improve JSDoc for Signal Forms Schema types
Improved documentation for Schema, SchemaFn, and SchemaOrSchemaFn types with clearer descriptions and usage examples.
1 parent 356092a commit 193aa33

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,42 @@ export type MaybeSchemaPathTree<TModel, TPathKind extends PathKind = PathKind.Ro
433433
| SchemaPathTree<Exclude<TModel, undefined>, TPathKind>;
434434

435435
/**
436-
* Defines logic for a form.
436+
* A reusable schema that defines behavior and rules for a form.
437437
*
438-
* @template TValue The type of data stored in the form that this schema is attached to.
438+
* A `Schema` encapsulates form logic such as validation rules, disabled states, readonly states,
439+
* and other field-level behaviors.
440+
*
441+
* Unlike raw {@link SchemaFn}, a `Schema` is created using
442+
* the {@link schema} function and is cached per-form, even when applied to multiple fields.
443+
*
444+
* ### Creating a reusable schema
445+
*
446+
* ```typescript
447+
* interface Address {
448+
* street: string;
449+
* city: string;
450+
* }
451+
*
452+
* // Create a reusable schema for address fields
453+
* const addressSchema = schema<Address>((p) => {
454+
* required(p.street);
455+
* required(p.city);
456+
* });
457+
*
458+
* // Apply the schema to multiple forms
459+
* const shippingForm = form(shippingModel, addressSchema, {injector});
460+
* const billingForm = form(billingModel, addressSchema, {injector});
461+
* ```
462+
*
463+
* ### Passing a schema to a form
464+
*
465+
* A schema can also be passed as a second argument to the {@link form} function.
466+
*
467+
* ```typescript
468+
* readonly userForm = form(addressModel, addressSchema);
469+
* ```
470+
*
471+
* @template TModel Data type.
439472
*
440473
* @category types
441474
* @experimental 21.0.0
@@ -445,9 +478,21 @@ export type Schema<in TModel> = {
445478
};
446479

447480
/**
448-
* Function that defines rules for a schema.
481+
* A function that receives a {@link SchemaPathTree} and applies rules to fields.
449482
*
450-
* @template TModel The type of data stored in the form that this schema function is attached to.
483+
* A `SchemaFn` can be passed directly to {@link form} or to the {@link schema} function to create a
484+
* cached {@link Schema}.
485+
*
486+
* ```typescript
487+
* const userFormSchema: SchemaFn<User> = (p) => {
488+
* required(p.name);
489+
* disabled(p.email, ({valueOf}) => valueOf(p.name) === '');
490+
* };
491+
*
492+
* const f = form(userModel, userFormSchema, {injector});
493+
* ```
494+
*
495+
* @template TModel Data type.
451496
* @template TPathKind The kind of path this schema function can be bound to.
452497
*
453498
* @category types
@@ -458,7 +503,7 @@ export type SchemaFn<TModel, TPathKind extends PathKind = PathKind.Root> = (
458503
) => void;
459504

460505
/**
461-
* A schema or schema definition function.
506+
* A {@link Schema} or {@link SchemaFn}.
462507
*
463508
* @template TModel The type of data stored in the form that this schema function is attached to.
464509
* @template TPathKind The kind of path this schema function can be bound to.

0 commit comments

Comments
 (0)