Slide 1

Slide 1 text

Validating your JSON

Slide 2

Slide 2 text

About me → Anne-Julia Scheuermann → Creating Web-Applications since 2004 → Developing information literacy solutions for

Slide 3

Slide 3 text

Short Story API DB JSON

Slide 4

Slide 4 text

Short Story API DB JSON

Slide 5

Slide 5 text

Custom validation

Slide 6

Slide 6 text

Custom validation protected function isValidUnformatted(array $data) { if (empty($data['data'])) { return false; // no data sent } $data = $data['data']; $source = $data['source']; $style = empty($data['style']) ? null : $data['style']; $structure = $this->getStructure($source, $style); $allowedTopLevelFields = $this->getOtherValidFields(); $ignoredDataFields = $this->getIgnoredFields(); // source if (!empty($structure['source_data'])) { if (empty($data[$source])) { $this->errors[] = 'needed source data missing'; return false; } $notAllowed = array_diff_key($data[$source], array_flip($structure['source_data'])); // +300 lines of code more

Slide 7

Slide 7 text

Many lines of complex code

Slide 8

Slide 8 text

PHPMD warnings /!\ /** * @param array $data * * @Todo Fix suppressed warnings * @SuppressWarnings( * PHPMD.CyclomaticComplexity, * PHPMD.NPathComplexity * ) * @return bool */ protected function isValidUnformatted(array $data) { //...

Slide 9

Slide 9 text

Validation only on server side API DB JSON

Slide 10

Slide 10 text

Other technologies have solved this

Slide 11

Slide 11 text

DTD for SGML

Slide 12

Slide 12 text

XSD for XML (XHTML)

Slide 13

Slide 13 text

Short Story API DB JSON

Slide 14

Slide 14 text

JSON Schema → http://json-schema.org/

Slide 15

Slide 15 text

Hello World! 42 ”42” {} true

Slide 16

Slide 16 text

JSON Schema specification Validation Core

Slide 17

Slide 17 text

JSON Schema specification Validation Core

Slide 18

Slide 18 text

type {“type”: “string”} {“type”: “integer”} {“type”: “array”} {“type”: “object”} {“type”: “boolean”} {“type”: “null”} {“type”: “number”}

Slide 19

Slide 19 text

string

Slide 20

Slide 20 text

string ”Hello World!” ”42” {”type”: ”string”} 42

Slide 21

Slide 21 text

”php” string length { ”type”: ”string”, ”minLength”: 2, ”maxLength”: 3 } ”i

Slide 22

Slide 22 text

string pattern { ”type”: ”string”, ”pattern”: ”^and.+$” } ”iphone” ”android”

Slide 23

Slide 23 text

numeric

Slide 24

Slide 24 text

integer -1 42 0.5 “42” { ”type”: ”integer” }

Slide 25

Slide 25 text

number -1 42 0.5 2.99792458e8 { ”type”: ”number” }

Slide 26

Slide 26 text

number range 10 11 40 { ”type”: ”number”, “minimum”: 10, “maximum”: 40, “exclusiveMinimum”: true, “exclusiveMaximum”: false }

Slide 27

Slide 27 text

array

Slide 28

Slide 28 text

array [1, 2, 3, 4, 5] [1, “2”, {“3”:”4 and 5”}, [42]] {”type”: ”array”} {“this”: “is an object”}

Slide 29

Slide 29 text

array items [] [1, 2, 3, 4, 5] [1, 2, 3, 4, “5”] { ”type”: ”array”, “items”: {“type”: “number”} }

Slide 30

Slide 30 text

array length [] [1] [1, 2] { ”type”: ”array”, “minItems”: 1, “maxItems”: 2 } [1, 2, 3]

Slide 31

Slide 31 text

object

Slide 32

Slide 32 text

object { ”key”: ”value”, ”anotherKey”: ”anotherValue” } {”type”: ”object”} ”42” [“hello”, “world”]

Slide 33

Slide 33 text

{”author”: ”A. A. Milne”, ”year”: 1882} properties { ”type”: ”object”, ”properties”: { ”author”: {”type”: ”string”}, ”year”: {”type”: ”number”} } } {”year”: ”1882”} {”author”: ”A. A. Milne”} {}

Slide 34

Slide 34 text

{”author”: ”A. A Milne”, ”title”: 1882} required properties { ”type”: ”object”, ”properties”: { ”author”: {”type”: ”string”}, ”title”: {”type”: ”string”}}, ”required”: [”title”] } {} {”author”: ”A. A Milne”, ”title”: ”Winni the Pooh”, ”year”: 1926} {”author”: “A. A Milne”}

Slide 35

Slide 35 text

additional properties { ”type”: ”object”, ”properties”: { ”author”: {”type”: ”string”}, ”title”: {”type”: ”string”} }, ”additionalProperties”: false } {”title”: []} {} {”author”: ”A. A. Milne”, ”title”: ”Winni the Pooh”, ”year”: 1926} {”author”: ”A. A. Milne”, ”title”: ”Winni the Pooh”}

Slide 36

Slide 36 text

{”a”: ”aa”, “b”: “bb”} number of properties { ”type”: ”object”, ”minProperties”: 1, ”maxProperties”: 2 } {} {”a”: ”aa”} {”a”: ”aa”, “b”: “bb”, “c”: “cc”}

Slide 37

Slide 37 text

dependencies

Slide 38

Slide 38 text

{”name”: ”Alan”, “birthday”: “1882-01-18”} property dependencies { ”type”: ”object”, ”properties”: {“name”: {“type”: “string”}, “birthday”: {“type”: “string”}}, ”dependencies”: { “birthday”: [“name”]} } {”name”: ”Alan”} {“birthday”: “1882-01-18”}

Slide 39

Slide 39 text

{”name”: ”Alan”, “birthday”: “1882-01-18”} schema dependencies { ”type”: ”object”, ”properties”: { “birthday”: {“type”: “string”}}, ”dependencies”: { “birthday”: { “properties”: {“name”: {“type”: “string”}}}} } {”name”: ”Alan”} {“birthday”: “1882-01-18”}

Slide 40

Slide 40 text

{”PHPUnit”: ”testing”, “PHPCS”: “styling”} pattern properties { ”type”: ”object”, ”patternProperties”: { “^PHP”: {“type”: “string”}}, “additionalProperties”: {“type”: “integer”} } {”BeHat”: ”accepting”} {“PHPLOC”: 9001} {“LOC”: 9001}

Slide 41

Slide 41 text

boolean

Slide 42

Slide 42 text

boolean true false {”type”: ”boolean”} “true” 0

Slide 43

Slide 43 text

null

Slide 44

Slide 44 text

null null false {”type”: ”null”} “” 0

Slide 45

Slide 45 text

More features

Slide 46

Slide 46 text

“one” “eleven” 1 -11 Combining with oneOf, anyOf, allOf { ”anyOf”: [ {“type”: “string”, “maxLength”: 5}, {“type”: “number”, “minimum”: 0} ] }

Slide 47

Slide 47 text

not “42” {“key”: 42} 42 { ”not”: {“type”: “number”} }

Slide 48

Slide 48 text

available formats: email, date-time, hostname, ipv4, ipv6, uri ”[email protected]” ”ab@mail.” built-in formats { ”format”: ”email” }

Slide 49

Slide 49 text

JSON Schema specification Validation Core

Slide 50

Slide 50 text

title, description & default { ”title”: ”Expressive title”, ”description”: “Take time to explain”, ”default”: “Validators ignore this keyword” }

Slide 51

Slide 51 text

$schema { “$schema”: “http://json-schema.org/draft-04/schema#”, ”title”: ”Expressive title”, ”description”: “Take time to explain”, ”default”: “Validators ignore this keyword” }

Slide 52

Slide 52 text

definitions { “$schema”: “http://json-schema.org/draft-04/schema#”, “definitions”: { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] } } }

Slide 53

Slide 53 text

$ref & definitions { “$schema”: “http://json-schema.org/draft-04/schema#”, “definitions”: { "address": { "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] } } "type": "object", "properties": { "billing_address": { "$ref": "#/definitions/address" }, "shipping_address": { "$ref": "#/definitions/address" } } }

Slide 54

Slide 54 text

$ref & definitions { “$schema”: “http://json-schema.org/draft-04/schema#”, “description”: “This schema defines an address”, "type": "object", "properties": { "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] } { “$schema”: “http://json-schema.org/draft-04/schema#”, “description”: “This schema describes an order”, "type": "object", "properties": { "billing_address": { "$ref": "address.json#" }, "shipping_address": { "$ref": "address.json#" } } } address.json order.json

Slide 55

Slide 55 text

Breathe!

Slide 56

Slide 56 text

Validation Libraries

Slide 57

Slide 57 text

Validation Libraries geraintluff/jsv4-php 11 commits, 1 contributor hasbridge/php-json-schema 28 commits, 2 contributors, not yet feature complete justinrainbow/json-schema 202 commits, 21 contributors

Slide 58

Slide 58 text

Integration of schema validation

Slide 59

Slide 59 text

Preparing validation with json-schema // Get the schema as object $retriever = new JsonSchema\Uri\UriRetriever; $schema = $retriever->retrieve('file://' . realpath('schema.json')); // Resolve references $refResolver = new JsonSchema\RefResolver($retriever); $refResolver->resolve($schema, 'file://' . __DIR__);

Slide 60

Slide 60 text

Validation with json-schema /** * Validate json against schema * * @param string $data * @param string $schema * * @return bool */ public function isValid($data, $schema) { $data = json_decode($data); $this->validator->check($data, $schema); $this->errors = $this->validator->getErrors(); return $this->validator->isValid(); }

Slide 61

Slide 61 text

The benefits of using JSON Schema

Slide 62

Slide 62 text

Less code /** * Validate json against schema * * @param string $data * @param string $schema * * @return bool */ public function isValid($data, $schema) { $data = json_decode($data); $this->validator->check($data, $schema); $this->errors = $this->validator->getErrors(); return $this->validator->isValid(); }

Slide 63

Slide 63 text

No custom validation

Slide 64

Slide 64 text

Complexity Complexity of validation and document structure is independent

Slide 65

Slide 65 text

Validation on client and server side API DB JSON

Slide 66

Slide 66 text

Implementation in many languages PHP, JavaScript, Java, Python, Ruby, Perl, .NET, C, Haskell, Erlang, Go

Slide 67

Slide 67 text

Better user experience Clear, human- and machine-readable documentation of our data structure

Slide 68

Slide 68 text

Thank You! ● http://json-schema.org/ ● http://tools.ietf.org/html/draft-zyp-json-schema-04 ● https://en.wikipedia.org/wiki/JSON ● http://spacetelescope.github.io/understanding-json-schema/ ● https://packagist.org/packages/justinrainbow/json-schema ● http://getcomposer.org

Slide 69

Slide 69 text

Questions?