aurelia / validatejs

Enables expressive validation using decorators and/or a fluent API.
MIT License
22 stars 23 forks source link

Adding validation creates circular references blocking JSON serialization #68

Closed jadrake75 closed 8 years ago

jadrake75 commented 8 years ago

So I switched over to the validatejs in place of the validation Aurelia library.

I added some validation rules to one of my models. The code is very straight forward:

` setupValidation() {

    this.validator = new Validator(this.model)
        .ensure('number')
            .length({ minimum: 1, maximum: 25, message: this.i18n.tr('messages.numberInvalid')})
            .required( { message: this.i18n.tr('messages.numberRequired')});
    this.reporter = ValidationEngine.getValidationReporter(this.model);
    if( this.observer ) {
        this.observer.dispose();
    }
    this.observer = this.reporter.subscribe(this.handleValidation.bind(this));
}

`

I create a validator (this is called from attached() of my view-model/widget and I assign it to the model (the page model) and ensure there is a value of the string value that is at least one and less than 26 characters. Then I add a reporter and subscribe to it.

Ok so this works. The issue is, on edit in memory this model gets a

_ validationReporter _ (two underscores) assigned only on change. ie. if I open this panel with the model, the validation reporter is not there. But if I edit it in some way this _ validationReporter _ object is added.

When I process my form to submit it to the server (a REST API) I call

JSON.stringify(model);

This caused the following stack

core-framework.js:5 ERROR [stamp-editor] TypeError: Converting circular structure to JSON at Object.stringify (native)

I tracked this down to this _ validationReporter _ on the model. If I simply delete it, the stringify works. So I can work around this, but this seems a bit unintuitive. ie. by adding validation we are adding a private object which is creating circular references which is potentially impacting serialization.

EisenbergEffect commented 8 years ago

@PWKad Whenever a property like this is added, it needs to be enumerable: false.

jdanyow commented 8 years ago

validation reporter no longer exists in the latest bits http://blog.durandal.io/2016/06/14/new-validation-alpha-is-here/