Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document custom keyword validation #165

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@ Options:
-v, --version Show version
```

## Custom keyword validation

Custom keyword validation can be achieved by extending spec vocab. E.g. to error on fields marked with `deprecated` keyword one can:

```ruby
class Deprecated < JSONSchemer::Keyword
def error(formatted_instance_location:, **)
"#{formatted_instance_location} is deprecated"
end

def validate(instance, instance_location, keyword_location, _context)
result(instance, instance_location, keyword_location, false)
end
end

JSONSchemer::Draft202012::Vocab::VALIDATION['deprecated'] = Deprecated
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down