-
Notifications
You must be signed in to change notification settings - Fork 14
/
advanced.coffee
57 lines (42 loc) · 1.32 KB
/
advanced.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
JSCK = require("../../src/index")
# using a schema that declares a URI with "id"
jsck = new JSCK.draft4
$schema: "http://json-schema.org/draft-04/schema#"
id: "urn:jsck.examples.advanced#"
definitions:
user:
type: "object"
required: ["login"]
properties:
login:
type: "string"
pattern: "^[\\w\\d_]{3,32}$"
email:
type: "string"
validator = jsck.validator(uri: "urn:jsck.examples.advanced#")
{valid} = validator.validate
login: "automatthew"
email: "[email protected]"
console.log "Schema with id:", valid
# validating against a subschema using a JSON Pointer
validator = jsck.validator "urn:jsck.examples.advanced#/definitions/user"
{valid} = validator.validate
login: "automatthew"
email: "[email protected]"
console.log "Schema identified by JSON Pointer:", valid
# Adding multiple schemas
#
# You can instantiate JSCK with multiple schemas or add them later
#
# Instantiation:
# validator = new JSCK(schema1, schema2, schema3)
jsck.add
id: "urn:jsck.examples.user_list#"
type: "array"
items: {$ref: "urn:jsck.examples.advanced#/definitions/user"}
validator = jsck.validator "urn:jsck.examples.user_list#"
{valid} = validator.validate [
{ login: "dyoder" }
{ login: "automatthew" }
]
console.log "Multiple schemas:", valid