Skip to content

Instantly share code, notes, and snippets.

@amaralrfl
Last active July 29, 2018 19:07
Show Gist options
  • Save amaralrfl/8107b2b83fd0aa5d4ee0d678694778b5 to your computer and use it in GitHub Desktop.
Save amaralrfl/8107b2b83fd0aa5d4ee0d678694778b5 to your computer and use it in GitHub Desktop.
Classe exemplo de uso do json schema com python
from jsonschema import validate
schema = {
"title": "Person",
"type": "object",
"required": [ "firstName", "lastName" ],
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0
},
"maritalStatus": {
"type": "string",
"enum": ["Solteirx", "Casadx", "Divorciadx" ]
}
}
}
#JSON Válido
validate({"firstName": "Rafael", "lastName": "Amaral", "age": 25, "maritalStatus": "Solteirx"}, schema)
#JSON Inválido - Idade passada é uma string
validate({"firstName": "Rafael", "lastName": "Amaral", "age": "vinte e cinco", "maritalStatus": "Casadx"}, schema)
#JSON Inválido - Não foi passado o campo obrigatório firstName
validate({"lastName": "Amaral", "age": 25, "maritalStatus": "Divorciadx"}, schema)
#JSON Inválido - Não foi passado o campo obrigatório lastName
validate({"firstName": "Rafael", "age": 25, "maritalStatus": "Divorciadx"}, schema)
#JSON Inválido - Não foi passado um estados cívil esperado
validate({"firstName": "Rafael", "lastName": "Amaral", "age": 25, "maritalStatus": "Viúvx"}, schema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment