Skip to content

Instantly share code, notes, and snippets.

@lbragstad
Created April 25, 2017 19:54
Show Gist options
  • Save lbragstad/6eca287b684bc34da6908ce1afdee03f to your computer and use it in GitHub Desktop.
Save lbragstad/6eca287b684bc34da6908ce1afdee03f to your computer and use it in GitHub Desktop.
Keystone API Validation using JSD
endpoint_schema = Endpoint(required=True)
resource_to_validate = endpoint_schema.__class__.__name__.lower()
@validation.validated(endpoint_schema.json(), resource_to_validate)
endpoint_schema = Endpoint(required=True)
@validation.validated(endpoint_schema) # validation handles calling .json()
# and figuring out the class name
{
"additionalProperties": true,
"properties": {
"description": {
"type": [
"string",
"null"
]
},
"domain_id": {
"maxLength": 64,
"minLength": 1,
"pattern": "^[a-zA-Z0-9-]+$",
"type": "string"
},
"enabled": {
"enum": [
true,
false
],
"type": "boolean"
},
"name": {
"maxLength": 64,
"minLength": 1,
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
import jsd
class Project(jsd.Object):
description = jsd.String()
domain_id = jsd.String()
enabled = jsd.Boolean()
name = jsd.String(required=True)
{
"properties": {
"description": {
"type": [
"string",
"null"
]
},
"domain_id": {
"type": [
"string",
"null"
]
},
"enabled": {
"type": [
"boolean",
"null"
]
},
"name": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
{
"additionalProperties": true,
"minProperties": 1,
"properties": {
"description": {
"type": [
"string",
"null"
]
},
"domain_id": {
"maxLength": 64,
"minLength": 1,
"pattern": "^[a-zA-Z0-9-]+$",
"type": "string"
},
"enabled": {
"enum": [
true,
false
],
"type": "boolean"
},
"name": {
"maxLength": 64,
"minLength": 1,
"type": "string"
}
},
"type": "object"
}
import jsd
class Url(jsd.String):
min_length = 0
max_length = 255
pattern = '^[a-zA-Z0-9-]+$'
class Endpoint(jsd.Object):
enabled = jsd.Boolean()
interface = jsd.String(required=True)
region_id = jsd.String()
service_id = jsd.String(required=True)
url = Url(required=True)
import jsd
class Url(jsd.String):
min_length = 0
max_length = 255
pattern = '^[a-zA-Z0-9-]+$'
url = {
'type': 'string',
'minLength': 0,
'maxLength': 225,
'pattern': '^[a-zA-Z0-9-]+$'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment