You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aes/util/jaw/README.md
+135-2
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,142 @@ Jaw is a data controller that wraps JSON objects with JSON-schema. It provides a
16
16
17
17
## Use
18
18
19
-
Load the module by creating a reference.
19
+
Init Jaw with a [schema](http://json-schema.org/) generate an instance that creates all the required properties:
20
20
21
-
var Jaw = Sky.getUtil("jaw")
21
+
var schema = {
22
+
"title": "Test",
23
+
"type" : "object",
24
+
"properties": {
25
+
"a" : {"type": "string"},
26
+
"b" : {"type": "object"}
27
+
},
28
+
"required" : ["a"]
29
+
}
30
+
31
+
var Jaw = Sky.getUtil("jaw").init( schema )
32
+
33
+
console.log( Jaw.get() )
34
+
35
+
Output: `{ a : "" }`
36
+
37
+
You can also generate **all** properties by passing a `boolean` like so:
38
+
39
+
var Jaw = Sky.getUtil("jaw").init( schema, true )
40
+
41
+
console.log( Jaw.get() )
42
+
43
+
Output: `{ a : "", b : {} }`
44
+
45
+
46
+
**Or** you can wrap Jaw arround existing objects. Keep in mind that these will be validated against the given schema. Jaw will try and generate any requirements that are missing or needed:
47
+
48
+
// schema from above
49
+
50
+
var obj = { x : 0 }
51
+
Jaw = new jaw( schema, obj )
52
+
53
+
console.log( Jaw.get() )
54
+
console.log( Jaw.get() === obj)
55
+
56
+
57
+
Output: `{ a : "", x : 0 }`, `true`
58
+
59
+
### Set and Get
60
+
We can set property values using simple dot notation. The `set()` function can generate deep structures on the fly. The `get()` command is used to retrieve values.
61
+
62
+
// Schema from above
63
+
Jaw.set( "b.c.0.d", 10 )
64
+
65
+
console.log( Jaw.get('b.c.0') )
66
+
67
+
Output: `{ d : 10 }`
68
+
69
+
Not specifying a path always returns the complete object:
70
+
71
+
console.log( Jaw.get() )
72
+
73
+
Output: `{ a : "", b : { c : [{ d : 10 }] } }`
74
+
75
+
76
+
### Validate
77
+
78
+
There are two ways of validating depending on the type of return you're after. `validate()` returns an array of errors and `isValid()` returns a boolean:
79
+
80
+
var result = Jaw.wrap( obj ).isValid()
81
+
82
+
console.log( result )
83
+
84
+
Output: `true` (No errors, object is succesfully validated against schema.)
85
+
86
+
var result = Jaw.wrap( obj ).validate()
87
+
88
+
console.log( result )
89
+
90
+
Output: `[]` (No errors, object is succesfully validated against schema.)
91
+
92
+
93
+
## Errors
94
+
All errors are saved into an error stack you can access with the `errors()` function:
95
+
96
+
console.log( Jaw.getErrors() )
97
+
98
+
Output: `[]` (No errors)
99
+
100
+
101
+
Advanced Use
102
+
------------
103
+
104
+
Besides the getters and setters Jaw implements some familiar tools for easy manipulation of existing properties.
0 commit comments