Sub topic 12
Extract and reuse schemas
This chapter assumes you can already write a schema function with form(), and that you know the built-in and custom validators from earlier chapters. You will move rules out of one form so that several forms or fields can share them.
Why extract a schema
A schema function is the callback you pass to form(). As a form grows, that callback gets long. Two problems appear:
- The same rules get copied. A billing address and a shipping address need identical checks.
- The callback mixes unrelated concerns, so it becomes hard to read.
Signal Forms lets you pull rules into a named schema. Then you attach that schema to any field whose value has a matching shape.
Create a schema with schema()
schema() takes a type parameter and a function that receives a path. The function body looks the same as the one you give to form(). The result is a value you can store in a constant and export.
import { schema, required, minLength } from '@angular/forms/signals';
export interface Address {
street: string;
city: string;
postcode: string;
}
export const addressSchema = schema<Address>((path) => {
required(path.street, { message: 'Street is required' });
required(path.city, { message: 'City is required' });
required(path.postcode, { message: 'Postcode is required' });
minLength(path.postcode, 4, { message: 'Postcode is too short' });
});The type parameter matters. It tells TypeScript which properties path has, so a typo such as path.postcod fails at compile time.
Attach it with apply()
Inside another schema function, call apply(path, schema). The rules in the schema then run against the field at that path.
import { signal } from '@angular/core';
import { form, apply, required } from '@angular/forms/signals';
import { Address, addressSchema } from './address-schema';
const emptyAddress: Address = { street: '', city: '', postcode: '' };
const model = signal({
name: '',
billing: { ...emptyAddress },
shipping: { ...emptyAddress },
});
const checkoutForm = form(model, (path) => {
required(path.name, { message: 'Name is required' });
apply(path.billing, addressSchema);
apply(path.shipping, addressSchema);
});Each address now has its own state. checkoutForm.billing.postcode().errors() and checkoutForm.shipping.postcode().errors() are separate signals, even though one schema defines both sets of rules.
Compose and combine
You can call apply() several times on one path, so small schemas combine. A contactSchema and an auditSchema can both apply to the same object. You can also call apply() inside a schema. That lets an orderSchema include addressSchema and be reused itself.
You can still add rules next to an applied schema. The next example applies addressSchema to the shipping address, then adds a rule that only the shipping address needs:
const shippingForm = form(model, (path) => {
apply(path.shipping, addressSchema);
required(path.shipping.city, { message: 'We need a delivery city' });
});Both rules run, and the field is invalid if either one fails.
When to extract
- Extract when two or more fields share the same rules.
- Extract when a group of rules has a clear name, such as "address" or "credentials".
- Keep one-off rules inline. A schema used once adds a file to open without making the code any clearer.
Signal Forms is experimental in Angular 21, so check the release notes for renamed helpers when you upgrade.
Exercise
Your form has home and work fields, both typed as { street: string; city: string }. How do you give both the same required-field rules without copying code?
Show answer
Create a schema with const placeSchema = schema<{ street: string; city: string }>((path) => { required(path.street); required(path.city); });. Then, inside the form() schema function, call apply(path.home, placeSchema) and apply(path.work, placeSchema). Each field keeps its own state, such as errors and touched.