Skip to content

Instantly share code, notes, and snippets.

@joaquimadraz
Created January 16, 2016 12:15
Show Gist options
  • Save joaquimadraz/b11416227f339044b24c to your computer and use it in GitHub Desktop.
Save joaquimadraz/b11416227f339044b24c to your computer and use it in GitHub Desktop.
object = {
first_name: 'Joaquim',
birth_date: '1989-0',
address: {
line_one: 'Lisboa',
post_code: '1100',
country_code: 'PT'
}
}
schema = Compel.hash.keys({
first_name: Compel.string.required,
last_name: Compel.string.required,
birth_date: Compel.datetime,
address: Compel.hash.keys({
line_one: Compel.string.required,
line_two: Compel.string.default('-'),
post_code: Compel.string.format(/^\d{4}-\d{3}$/).required,
country_code: Compel.string.in(['PT', 'GB']).default('PT')
})
})
schema.validate(object)
=> <Compel::Result:0x007f82a1b053f8
@errors=
{"last_name"=>["is required"],
"birth_date"=>["'1989-0' is not a parsable datetime with format: %FT%T"],
"address"=>{"post_code"=>["must match format ^\\d{4}-\\d{3}$"]}},
@valid=false,
@value=
{"first_name"=>"Joaquim",
"birth_date"=>"1989-0",
"address"=>{"line_one"=>"Lisboa", "post_code"=>"1100", "country_code"=>"PT", "line_two"=>"-"},
"errors"=>
{"last_name"=>["is required"],
"birth_date"=>["'1989-0' is not a parsable datetime with format: %FT%T"],
"address"=>{"post_code"=>["must match format ^\\d{4}-\\d{3}$"]}}}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment