We just presented you 40 most used Ruby gems that can be very helpful for you. And today we are going to discuss the validation of the structure and basic checks, which is meant to ensure that given data input is safe to work with. So, yes, today’s post will be about dry-schema Ruby gem in details.
The sole purpose of dry-schema is to process and validate data input. There are many use cases for this library, most common ones are checking HTTP parameters (either GET or POST) and JSON data structures. If you’re familiar with dry-validation, it’s the same thing but without complex features like high-level rules or complex predicates.
When you apply a schema 4 things happen:
- Known keys are coerced, if needed, to symbols, unknown keys are rejected
- (optional) Before trying to apply coercion, values can be checked using pre-coercion rules
- Values are coerced, when needed, based on the type specifications in the schema
- Keys and values are validated based on rules defined in the schema
When you apply a schema, keys are always processed.
What it means is that unknown keys are always rejected. In case of
JSON schemas, keys are also coerced to symbols. This is handled by a separate step now, that used a new KeyMap object. There will be more features built on top of it, like producing error messages about unexpected keys (something that was requested many times before).
Interested? Feel free to check more here.
And let’s explore together ValueSemantics, that is a Ruby gem for making value classes.