3 ms·
I came across contracts some time ago and found it very appealing and inspiring, but for all my professional projects I am successfully using my own library sch
by Andi 15y ago
I came across contracts some time ago and found it very appealing and inspiring, but for all my professional projects I am successfully using my own library schema.js ( https://github.com/akidee/schema.js https://github.com/akidee/schema.js ) - you can use it in pure JavaScript, too.
(Usually I do not validate forms on the client-side, since the data must be validated on the server-side anyway. And with schema.js, you can add custom localized error messages.)
Furthermore, schema.js has several modes that allow tolerant adaptation of values, so that they can be casted. "5", for example, is casted to 5. But you can switch to strict validation mode as well.
You can do everything, that contracts allows, with schema.js, too.
The best is that, with JSON schema, 1) you define secure validations, since with the default properties you do not forget important constraints, and 2) you do not need to test your validators any more.
Conclusion: I agree that such features must make it to the core of a language. Probably you can find some useful ideas in JSON schema and schema.js. I found the JSON vocabulary to be very useful to validate JS data.