Nested object arrays into Schema that have to be validated

91 views
Skip to first unread message

tghcastro

unread,
Sep 13, 2017, 11:56:42 AM9/13/17
to Ruby JSON Schema Library
Hi, guys!

I'm having some troubles to validate some array properties from an object. Probably I've done something wrong!

The HASH that must be validated is:

hash = {
:entitlements => [
{
:id => '111998',
:title => 'Entitlement 1',
:assetId => '000777'
},
{
:id => '111999',
:title => 'Entitlement 2',
:assetId => '000777'
}
],
:offers => [
{
:id => '111001',
:title => 'Offer 1'
            :price => 100
},
{
:id => '111002',
:title => 'Offer 2'
}
]
} 

I'm trying to validate:
  1. The main object has two properties (entitlements and offers) 
  2. All 'entitlements' objects have their properties (id, title and assetId)
  3. All 'offers' objects have their properties (id, title and price)
    1. Price is not mandatory

And this is the schema I tried to use:

options_schema = {
    :type => 'object',
:required => %w"offers entitlements",
:properties => {
:entitlements => {
:type => 'array',
:properties => {
:item => {
:type => 'object',
:required => %w"id",
}
}
}
        :offers => {
:type => 'array',
:properties => {
:item => {
:type => 'object',
:required => %w"id",
}
}
}
}
}

When I run the "JSON::Validator.validate(options_schema, hash)", its always returns TRUE.
How can I validate object arrays on properties?

Thanks!

Thiago de Castro

Iain Beeston

unread,
Sep 13, 2017, 12:59:48 PM9/13/17
to tghcastro, Ruby JSON Schema Library
Two things:

1. You probably want "additionalProperties: false" on your object types, or it will allow any property
2. Your item types should have "items" not "properties"

I recommend reading the Understanding JSON Schema ebook, it might help

--
You received this message because you are subscribed to the Google Groups "Ruby JSON Schema Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-json-sche...@googlegroups.com.
To post to this group, send email to ruby-jso...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-json-schema/8050efcd-fa77-45ac-bf39-85d8a033a39d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages