Skip to content

Exact return types for fields and members return value #11

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions