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

Add support (or README note) for custom keyword validation #195

Open
ekzobrain opened this issue Aug 21, 2024 · 2 comments
Open

Add support (or README note) for custom keyword validation #195

ekzobrain opened this issue Aug 21, 2024 · 2 comments

Comments

@ekzobrain
Copy link

We need to extend schema validation with custom keywords. We may describe them with additional vocabularies, but we also need to make an implementation for them to be validatable. How may it be implemented? It would be great to have an official support for extending validation keyword implementation. May be it is already possible, and a README paragraph would be sufficient.
For example, we need to add support for this: https://github.com/ajv-validator/ajv-keywords?tab=readme-ov-file#uniqueitemproperties

@ekzobrain ekzobrain changed the title Add support (or README note) for custom keyword validation (uniqueitemproperties) Add support (or README note) for custom keyword validation Aug 21, 2024
@davishmcclurg
Copy link
Owner

There is some limited support for custom keywords. It's undocumented because it's not well supported, but you can check out the tests for examples, eg:

def test_it_validates_correctly_custom_keywords
options = {
keywords: {
'ignored' => nil,
'even' => lambda do |data, curr_schema, _pointer|
curr_schema.fetch('even') == data.to_i.even?
end
}
}
schema = JSONSchemer.schema({ 'even' => true }, **options)
assert(schema.valid?(2))
refute(schema.valid?(3))
options = {
keywords: {
'two' => lambda do |data, curr_schema, _pointer|
if curr_schema.fetch('two') == (data == 2)
[]
else
['error1', 'error2']
end
end
}
}
schema = JSONSchemer.schema({ 'two' => true }, **options)
assert_equal([], schema.validate(2).to_a)
errors = schema.validate(3).map { |error| error.fetch('type') }
assert_equal(['error1', 'error2'], errors)
refute(schema.valid?(3))
end

We may describe them with additional vocabularies, but we also need to make an implementation for them to be validatable. How may it be implemented? It would be great to have an official support for extending validation keyword implementation.

Ideally, I'd like to support custom keywords via custom vocabularies, but I have not done any work on that. Please let me know if you're interested in contributing!

@davishmcclurg
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants