Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
Update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
rightlag committed Aug 8, 2017
1 parent facafb6 commit c28723e
Showing 1 changed file with 32 additions and 59 deletions.
91 changes: 32 additions & 59 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,42 @@ Given a JSON Schema document, `aptos` can validate client-submitted data to ensu

```json
{
"title": "Product",
"title": "Person",
"type": "object",
"definitions": {
"geographical": {
"title": "Geographical",
"description": "A geographical coordinate",
"type": "object",
"properties": {
"latitude": { "type": "number" },
"longitude": { "type": "number" }
}
}
},
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "number"
},
"name": {
"firstName": {
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
"dimensions": {
"title": "Dimensions",
"type": "object",
"properties": {
"length": {"type": "number"},
"width": {"type": "number"},
"height": {"type": "number"}
},
"required": ["length", "width", "height"]
"lastName": {
"type": "string"
},
"warehouseLocation": {
"description": "Coordinates of the warehouse with the product",
"$ref": "#/definitions/geographical"
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["id", "name", "price"]
"required": ["firstName", "lastName"]
}
```

Validation keywords such as `uniqueItems`, `required`, and `minItems` can be used in a schema to impose requirements for successful validation of an instance.
[Validation keywords](http://json-schema.org/latest/json-schema-validation.html#rfc.section.6) such as `minimum` and `required` can be used in a schema to impose requirements for successful validation of an instance.

| Valid Instance :heavy_check_mark: | Invalid Instance :heavy_multiplication_x: |
|-------------------------------------------------------|-------------------------------------------|
| `{"firstName": "John", "lastName": "Doe", "age": 42}` | `{"firstName": "John", "age": -15}` |

`aptos` can validate client-submitted data using either the CLI or the API:

### CLI

$ aptos validate -instance INSTANCE SCHEMA

| Successful Validation | Unsuccessful Validation |
|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| ![](https://user-images.githubusercontent.com/2184329/29053486-5c787966-7bbe-11e7-8fd3-4cb51d87d7d9.png =600x) | ![](https://user-images.githubusercontent.com/2184329/29053538-afcce9c6-7bbe-11e7-8be5-61ac1d876fc1.png =600x) |

### API

```python
import json
Expand All @@ -107,23 +89,14 @@ from aptos.visitor import ValidationVisitor
with open('/path/to/schema') as fp:
schema = json.load(fp)
component = SchemaParser.parse('/path/to/schema')
# Valid client-submitted data (instance)
# Invalid client-submitted data (instance)
instance = {
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
'firstName': 'John'
}
component.accept(ValidationVisitor(instance))
try:
component.accept(ValidationVisitor(instance))
except AssertionError as e:
print(e) # instance {'firstName': 'John'} is missing required property 'lastName'
```

## Structured Message Generation
Expand Down

0 comments on commit c28723e

Please sign in to comment.