adopted-ember-addons / adopted-ember-addons/validated-changeset

newValue received in validation for moment property is plain object instead of moment instance

Abierto
#193 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
TypeScript
Estrellas
38
Forks
27
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

I'm using `ember-changeset` v4.1.2 (`validated-changeset` v1.3.4) and have an ES6 class with a tracked property whose value is either `null` or a `moment` instance. If that property on an instance of the class is set to a `moment` instance and I create a Changeset around the object, when I call the object's `.validate()` method the value passed into the validator as the `newValue` is a plain object with the properties of the `moment` instance copied onto it. The validator checks the value's `.isValid()` method, which is part of the `moment` object but is not present on the `newValue` received by the validator. The issue seems to be caused by the [`unwrap` method](https://github.com/validated-changeset/validated-changeset/blob/c1c95b729aae80fd30b30b691202a94c974a3309/src/utils/object-tree-node.ts#L106) creating a plain object instead of returning the original class.

Repro:

```js
import moment from 'moment';

class FormData {
date = moment();
}

const formData = new FormData();
const validator = ({ newValue }) => {
return moment.isMoment(newValue) && newValue.isValid();
};
const changeset = Changeset(formData, validator);

changeset.validate('date'); // results in `TypeError: newValue.isValid is not a function` being thrown.
```

Perhaps I'm thinking about this in the wrong way, but it seems reasonable to expect that the `newValue` provided to a validator has the same signature as the underlying value, otherwise validator functions lose access to methods that were present on the original data type that are often useful as part of the validation routine.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.