To define the schema, currently we have to define types for nested elements so that extra fields would error. But, this approach gets a bit more verbose.
type FormSchema = ObjectSchema<PartialForm<FormType>>;
type FormSchemaFields = ReturnType<FormSchema['fields']>;
type MetaType = NonNullable<FormType['meta']>;
type MetaSchema = ObjectSchema<PartialForm<MetaType>>;
type MetaSchemaFields = ReturnType<MetaSchema['fields']>;
type CollectionType = NonNullable<NonNullable<FormType['collections']>>[number];
type CollectionSchema = ObjectSchema<PartialForm<CollectionType>>;
type CollectionSchemaFields = ReturnType<CollectionSchema['fields']>;
type CollectionsSchema = ArraySchema<PartialForm<CollectionType>>;
type CollectionsSchemaMember = ReturnType<CollectionsSchema['member']>;
const schema: FormSchema = {
fields: (): FormSchemaFields => ({
name: [requiredStringCondition],
meta: ({
fields: (): MetaSchemaFields => ({
age: [requiredCondition, greaterThanCondition(12)],
job: [],
}),
}),
collections: {
keySelector: (col) => col.clientId,
member: (): CollectionsSchemaMember => ({
fields: (): CollectionSchemaFields => ({
clientId: [],
date: [],
title: [requiredStringCondition],
}),
}),
},
}),
};
To define the schema, currently we have to define types for nested elements so that extra fields would error. But, this approach gets a bit more verbose.