indicative icon indicating copy to clipboard operation
indicative copied to clipboard

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

Open wkrueger opened this issue 3 years ago • 0 comments

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...

wkrueger avatar Jul 01 '21 17:07 wkrueger