Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

When a property is cast, the source object is mutated. #266

Open
wkrueger opened this issue Jul 1, 2021 · 0 comments
Open

When a property is cast, the source object is mutated. #266

wkrueger opened this issue Jul 1, 2021 · 0 comments

Comments

@wkrueger
Copy link

wkrueger commented Jul 1, 2021

Why this feature is required (specific use-cases will be appreciated)?

One of the main reasons (I believe) of indicative's schema format is that it is serializable. So, the same validation schema can be used on the backend, and in frontend forms.

If this lib is expected to be used on frontend, it is also expected to be used with React, and react expects objects not to be mutated.

const source = { someNumber: "3" }
const schema = { someNumber: "number" }
const cast = await validateAll(source, schema)
// in the end of this code, source is { someNumber: 3 }

I'd expect the source not to be mutated, and cast to contain a copy of source with the casts applied.

Have you tried any other work arounds?

In my project, the workaround was to wrap indicative with immer. Something like this.

import immer from 'immer'

immer(
  source,
  async (draft) => {
    await validateAll(draft, validationSchema)
  },
  (patches) => {
    patches.forEach((patch) => {
      onChange( ... )
    }
  }
)

Are you willing to work on it with little guidance?

I could, but it may take some time...

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

No branches or pull requests

1 participant