-
Notifications
You must be signed in to change notification settings - Fork 67
Description
I have an issue with using oneOf and after_property_validation.
after_property_validation is run after each property. In the case where there is a oneOf and subschemas with the same property name but a different format, the value in the data is replaced by the first format and therefore is corrupted for the next schema.
The data is being changed globally rather than discarded once the oneOf doesnt match that schema.
A contrived example is below:
require 'json_schemer'
convert_date = proc do |data, property, property_schema, _|
if data[property] && property_schema.is_a?(Hash) && property_schema['format'] == 'date'
data[property] = Date.iso8601(data[property])
elsif data[property] && property_schema.is_a?(Hash) && property_schema['format'] == 'rfc2822'
data[property] = Date.rfc2822(data[property])
end
end
schema = {
'oneOf' => [
{
'properties' => {
'myclass' => {
'const' => 'iso8601'
},
'start_date' => {
'type' => 'string',
'format' => 'date'
},
'email' => {
'format' => 'email'
}
}
},
{
'properties' => {
'myclass' => {
'const' => 'rfc2822'
},
'start_date' => {
'type' => 'string',
'format' => 'rfc2822'
},
'email' => {
'format' => 'email'
}
}
}
]
}
validator= JSONSchemer.schema(
schema,
after_property_validation: [convert_date]
)
data = { 'myclass' => 'rfc2822', 'start_date' => '2020-09-03', 'email' => 'example@example.com' }
validator.valid?(data)
puts data.inspect
This will fail when the rfc822 format is hit because the data is already converted to a Date.
Is that expected behavior? I expected that the values were sent into the subschema (and converted) but then discarded when it didn't match the oneOf subschema so it came as a bit of a surprise.
I think there may need to be a need for an after_schema_validation to cover scenarios like this. I think I will be able to put together something based on the annotations.