Skip to content

Exact return types for fields and members return value #11

@tnagorra

Description

@tnagorra

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],
                }),
            }),
        },
    }),
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions