Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created October 9, 2025 14:10
Show Gist options
  • Save Armenvardanyan95/22af047d5ed87eb48d52500de89bbddc to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/22af047d5ed87eb48d52500de89bbddc to your computer and use it in GitHub Desktop.
export interface ProductSchema {
title: string;
description: string;
price: number;
}
// define a validation schema
export const productSchema = schema<ProductSchema>(ctx => {
required(ctx.title);
min(ctx.price, 0);
});
// use it in a component
@Component({
template: `
<div>
<h2>Create Product</h2>
<input [control]="form.title" placeholder="Title" />
<input [control]="form.description" placeholder="Description" />
<input type="number" [control]="form.price" placeholder="Price" />
@if (form.price().errors().at(0)?.kind === 'min') {
<p>Price must be positive.</p>
}
</div>
`,
})
export class CreateProductComponent {
product = signal<ProductSchema>({
title: '',
description: '',
price: 0,
});
form = form(this.product, (ctx) => {
apply(ctx, productSchema);
});
}
// then another component
export class EditProductComponent {
product = signal<ProductSchema>({
title: '',
description: '',
price: 0,
});
form = form(this.product, (ctx) => {
apply(ctx, productSchema);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment