Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Plugin: No rules reset possible? #417

Open
skeadr opened this issue Nov 7, 2024 · 2 comments
Open

Validation Plugin: No rules reset possible? #417

skeadr opened this issue Nov 7, 2024 · 2 comments

Comments

@skeadr
Copy link

skeadr commented Nov 7, 2024

Hello.

I'm facing issues with the following use case.

A configuration editor component for JSON data for different products.

  • It displays a form that is backed by a hookstate with the validation plugin
  • Displayed fields are dynamically displayed depending on the data. E.G. number fields get a number validation
  • json for the data is an argument for the editor

Product A has $.daysToDelivery
Product B has $.daysSinceLastAccident

If I switch between those to products and state.set(newData), and afterwards do state.valid(), I get the error for the other product, because the property is missing -> not a number.

I didn't find any way to "reset" the plugin that reruns the onCreate and resets the rules map.
I tried to hookstate(newData, validation()) for the global variable of the state, but get the HOOKSTATE-111

Working example of the problem

function Test() {
    const [data, setData] = useState<object>({})
    return (
        <div>
            <button onClick={() => setData({ daysToDelivery: 2 })}>Product A</button>
            <button onClick={() => setData({ daysSinceLastAccident: 3 })}>Product B</button>
            <TestEditor data={data} />
        </div>
    )
}

export let globalState = hookstate<any, Validation>({}, validation())
function TestEditor(props: { data: object }) {
    const { data } = props
    const state = useHookstate(globalState)

    useEffect(() => {
        //globalState = hookstate<any, Validation>(data, validation())
        state.set(data)
    }, [data])

    return (
        <>
            <br />
            {state.errors().map((error) => `${error.path}: ${error.message}`)}
            <br />
            <br />
            {Object.keys(state.get({ noproxy: true })).map((key) => (
                <TestNumberField propName={key} number={state.nested(key)} />
            ))}
        </>
    )
}

function TestNumberField(props: { propName: string; number: State<any, Validation> }) {
    const valueState = useHookstate(props.number)

    valueState.validate((value) => {
        const number = Number(value)
        if (isNaN(number)) return false
        return Number.isInteger(number)
    }, "not a number")

    return (
        <>
            {props.propName}
            <br />
            <input value={valueState.get()} onChange={(e) => valueState.set(e.target.value)} />
        </>
    )
}
@skeadr
Copy link
Author

skeadr commented Nov 7, 2024

I created a pull request with a solution that I'm working with:

#418

It's an added function that allows for the storeRules to be cleared. Not sure that's the right approach, feels kinda hacky. But it does solve my problem.

Didn't add a unit test for this. But all present tests aren't affected.

Is there another solution that I'm missing? Some way to reset the plugin (rerun onCreate) out of the box that I didn't see?

@skeadr
Copy link
Author

skeadr commented Nov 7, 2024

Additional question, also for validation plugin:

path.forEach((p) => {
    /*if (typeof p === "number") {
        p = "*" // limitation: support only validation for each element of array
    }*/
    result = result && result[p]
})

What's the reason for this limitations? Strictly performance?
I do have non homogenous arrays and this limitation prevents those.
I removed those lines in my copied validation plugin, and it seems to work well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant