Skip to content

[SUGGESTION] types.optional 's function should have a flag (additional to its Symbol) #2008

Description

@kebeclibre

Have a helper function that builds a little signal holding a "fields"'s value and other stuff.
==>> the value should be validated using owl's validation schemas

function buildField(params = {}) {
    const field = signal(params.value ?? null);
    if (params.validation) {
        field.required = params.required ?? true;
        field.isValid = computed(() => validateType(field(), params.validation).length === 0)
    } else {
        field.isValid = () => true;
    }
    return field;
}

with concrete usage:

myField =  buildField({ validation: t.string().optional(), required: false, value: "diamond" })

In this example we could have guessed the field was not required because the optional(undefined) already holds that information
Currently, the optionalSymbol is not exported, rightly so, but in user space there is no way to tell if the validator makes the value required or not

Maybe set a readonly flag on the validator ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions