feat: add ObjectValidator for hash/schema validation#25
Merged
Conversation
Introduces ValidatorRb.object(schema) for validating hashes against a map of field-level sub-validators, addressing #9. - Accepts symbol or string keys interchangeably between schema and input - Emits sub-validator errors with a composed key path (e.g. [:address, :zip]) - .strict rejects undeclared keys with :unknown_key - .partial / .pick / .omit return new instances, preserving flags - Nests arbitrarily (ObjectValidator inside ObjectValidator)
- README: adds Object Validators section covering schema basics, nested
error paths, and the strict/partial/pick/omit modifiers; drops hash
from the roadmap
- CHANGELOG: logs ObjectValidator under [Unreleased] with its schema
shape, key interop, composed path, and modifier semantics
- CLAUDE: adds ObjectValidator to the validator list, documents the
multi-error validation pattern (push directly to @validations +
flatten) used by ArrayValidator#of and the schema block, and the
nested-path composition shared across container validators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
ObjectValidator, a schema-based validator forHashvalues, completing the most-requested feature from the roadmap. Defined viaValidatorRb.object(schema), it validates each field through a sub-validator and re-emits errors with a composed key path (e.g.path: [:address, :zip]), which also enables arbitrary nesting of objects inside objects and objects inside arrays.String and symbol keys are interchangeable between schema and input — a common source of friction when validating payloads that cross JSON and Ruby boundaries. The API ships with Zod-style schema modifiers:
.strict(rejects undeclared keys with:unknown_key),.partial(treats every key as optional), and
.pick/.omit(derive a narrower schema). All three modifiers return a new validator instance, so a single base schema can be reused safely for login, update, and public-view variants without cross-contamination.Fixes #9
Type of Change
Checklist
Test Coverage
Added
spec/validator_rb/object_validator_spec.rbwith 26 examples covering: hash/non-hash inputs, symbol↔string key interop,:requiredreporting on missing keys, nested path composition,.strict/.partial/.pick/.omitsemantics, and immutability of derivedvalidators. Full suite: 192 examples, 0 failures, 100% line coverage, 0 RuboCop offenses (
bundle exec rspec && bundle exec rubocop).