ember-changeset-validations icon indicating copy to clipboard operation
ember-changeset-validations copied to clipboard

Promise-based validation example and best practices?

Open timmyomahony opened this issue 8 years ago • 2 comments

Hey,

Thanks for the great library. I've noticed that you can return a promise from a custom validator leading me to believe I could write validators that query a remote backend. The most common use-case for this would be checking for other instances of a model with the same value (unique email for example).

It would be great to see a more verbose example this. I'm not sure what is the best way of getting access to the data store as an injected dependency. From my understanding (based largely on this SO answer) you would need to declare the validator as an Object and register it with the container to be able to inject the store dynamically.

I may be overthinking this but would like to hear about some best practices when needing to interact with other models during validation.

timmyomahony avatar Mar 01 '17 18:03 timmyomahony

I am not sure about best practice, but the solution I came up with is using a function to create the validations and passing in the store. This works for me because I am creating the validations programmatically.
Twiddle

SuperCoolFrog avatar Mar 01 '17 22:03 SuperCoolFrog

Thanks @SuperCoolFrog I'll have a look at that and try it out

timmyomahony avatar Mar 04 '17 11:03 timmyomahony