Skip to content

Commit a67e11a

Browse files
author
Bruno Herfst
committed
1 parent da9247a commit a67e11a

File tree

9 files changed

+664
-36
lines changed

9 files changed

+664
-36
lines changed

Diff for: aes/patch/json.clone/package.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@extendscript/aes.patch.json.clone",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Adds the `clone()` method to JSON",
55
"main": "clone.js",
66
"scripts": {
@@ -26,14 +26,11 @@
2626
},
2727
"homepage": "https://github.com/ExtendScript/extendscript-modules#readme",
2828
"devDependencies": {
29-
"build-node-venv": "^1.0.3",
30-
"minimist": "^1.2.0",
29+
"build-node-venv": ">=1.0.3",
30+
"minimist": ">=1.2.0",
3131
"@extendscript/tap-es": ">=1.0.5"
3232
},
3333
"peerDependencies": {
34-
"@extendscript/aes.patch.json": "^1.0.0"
35-
},
36-
"dependencies": {
37-
"@extendscript/aes.patch.json": "^2.0.0"
34+
"@extendscript/aes.patch.json": ">=1.0.0"
3835
}
3936
}

Diff for: aes/patch/json.instantiate/test/test-required.jsx

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include '../node_modules/@extendscript/aes.patch.array.foreach/foreach.js'
2+
#include '../node_modules/@extendscript/aes.patch.array.isarray/isarray.js'
3+
#include '../node_modules/@extendscript/aes.patch.json/json.js'
4+
#include '../instantiate.js'
5+
6+
var schema = {
7+
"title": "Test",
8+
"type" : "object",
9+
"properties": {
10+
"a" : {"type": "string", "default": "hi"},
11+
"b" : {"type": "object"}
12+
},
13+
"required" : ["a"]
14+
};
15+
16+
var preset = JSON.instantiate( schema );
17+
18+
$.writeln( JSON.stringify(preset) === '{"a":"hi"}');

Diff for: aes/util/jaw/README.md

+135-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,142 @@ Jaw is a data controller that wraps JSON objects with JSON-schema. It provides a
1616

1717
## Use
1818

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:
2020

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.
105+
106+
**Any type**
107+
108+
- .delete ( `path` )
109+
- .clone ( `path` )
110+
111+
**Array types**
112+
113+
- .push ( `path` )
114+
- .pop ( `path` )
115+
- .unshift ( `path` )
116+
- .shift ( `path` )
117+
- .splice ( `path`, `index`, `clear`, `element`, `element`, `etc...` )
118+
119+
120+
## Chaining
121+
122+
You can chain commands together:
123+
124+
var result = Jaw
125+
.set( "b.c.0.d", 10 )
126+
.get( "b.c.0" );
127+
128+
console.log( result )
129+
130+
Output: `{ d : 10 }`
131+
132+
133+
## Validating a Schema
134+
135+
You can also validate Schemas:
136+
137+
var schemaIsValid = new jaw( schema ).isValid();
138+
139+
console.log( schemaIsValid )
140+
141+
Output: `true`
142+
143+
144+
## Generate Templates
145+
Get the default instance of the schema by calling `getTemplate`:
146+
147+
Jaw.getTemplate()
148+
149+
Output: `{ a : "" }`
150+
151+
var allProperties = true;
152+
Jaw.getTemplate( allProperties )
153+
154+
Output: `{ a : "", b : {} }`
22155

23156
## Test
24157

0 commit comments

Comments
 (0)