DEV Community

loading...

Discussion on: Fight Validations Spaghetti with Vest.

Collapse
larswww profile image
larswww

I've been thinking a lot about how to deal with validation in the simplest most consistent form possible and will take a look at vest!

Wanted to ask; what are your thoughts on JSON schema? Pro's/con's on using JSON schemas with a validation library to specify validation?

Collapse
ealush profile image
Evyatar Alush Author

JSON schemas are great. I think that schema validation tools give you the ability to validate stuff that are usually pretty hard, or just redundant.

The thing that's a bit harder to do with schema validations is integrating them as a part of your feature logic, for example validating a specific field upon interaction. Another thing that can be annoying is dealing with warn only validations (does a medium strength password match, or not match the schema, then? How do you know to present a warning there?).

Schemas are mostly useful when you want to validate your whole form at once (upon submit, for example). Validating as the user types is a bit of a hassle.

It is possible, though, to use both vest and the schema validation library of your choice. You are not coupled to enforce as your assertion library. You can base your validations on anything that either throws an error, or explicitly returns false for an invalid test - so whatever you're using now, you can keep using it within vest.

Collapse
larswww profile image
larswww

man thanks a lot that is really helpful perspective, esp. re useful for validating a whole form vs specific fields!

"You can base your validations on anything that either throws an error, or explicitly returns false" sounds really flexible :)

Thread Thread
ealush profile image
Evyatar Alush Author

Thanks :)
The intention is not flexibility (took me a while to realize flexibility isn't always a good thing), but to allow easy migration of existing validation logic. Validations are usually boolean based, so this helps with that.

Forem Open with the Forem app