#Signal forms / Array validation

6 messages · Page 1 of 1 (latest)

elfin pier
#

Let' say that I have the following data model

export interface Employee {
name: string;
email: string;
age: number;
}

export interface Enterprise {
name: string;
employees: Employee[];
}

And the following is a signal Instance of the above data model:
enterpriseData = signal<Enterprise>({
name: '',
employees: []
});

And here's the form instance:
enterpriseForm = form(this.enterpriseData, (schemaPath) => {
required(schemaPath.name);
minLength(schemaPath.name, 2);
required(schemaPath.employees);
});

I would like to add validation to the name, the age and the email in every employee instance. How can I do that ?

deft osprey
wraith flame
#

applyEach + validator schema

deft osprey
#
const employeeSchema = schema<Employee>((path) => {
  required(path.name);
  required(path.email);
  min(path.age, 18);
});

enterpriseForm = form(this.enterpriseData, (schemaPath) => {
    required(schemaPath.name);
    minLength(schemaPath.name, 2);
    required(schemaPath.employees);
    applyEach(schemaPath.employees, employeeSchema);
  });

Should something like this work? (I'm learning.)

wraith flame
#

yup

deft osprey
#

Thanks.
Put a smile on my assignment and I'll bring you an apple.