Skip to content

Commit f66c136

Browse files
committed
$func instruction to define function
1 parent 9f78fca commit f66c136

File tree

6 files changed

+350
-1
lines changed

6 files changed

+350
-1
lines changed

instructions/$func.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "$func",
3+
"description": "function definition",
4+
"keywords": ["$func", "$name", "$args"],
5+
"required": ["$func"],
6+
"evaluate": {
7+
"keywords": ["$name", "$args"],
8+
"validatorKeyword": "eval$func",
9+
"title": "function definition",
10+
"description": "defines anonymous or named function"
11+
},
12+
"schema": {
13+
"$name": {
14+
"type": "string",
15+
"anyOf": [
16+
{ "pattern": "^[A-Za-z_$][A-Za-z_$0-9]+$" },
17+
{ "format": "uuid" }
18+
]
19+
},
20+
"$args": {
21+
"type": "array",
22+
"minItems": 1,
23+
"items": {
24+
"anyOf": [
25+
{
26+
"type": "string",
27+
"pattern": "^[A-Za-z_$][A-Za-z_$0-9]+$"
28+
},
29+
{
30+
"type": "object",
31+
"minProperties": 1,
32+
"maxProperties": 1,
33+
"patternProperties": {
34+
"^[A-Za-z_$][A-Za-z_$0-9]+$": {
35+
"$ref": "http://json-schema.org/draft-04/schema#"
36+
}
37+
}
38+
}
39+
]
40+
}
41+
}
42+
}
43+
}

instructions/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = [
55
require('./$ref.json'),
66
require('./$data.json'),
77
require('./$if.json'),
8-
require('./$delay.json')
8+
require('./$delay.json'),
9+
require('./$func.json')
910
];

schema/evaluate.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,85 @@
261261
]
262262
}
263263
},
264+
{
265+
"if": {
266+
"required": [
267+
"$func"
268+
]
269+
},
270+
"then": {
271+
"title": "instruction $func",
272+
"allOf": [
273+
{
274+
"title": "evaluate properties",
275+
"description": "evaluates all or some keywords using this (full) schema, properties-scripts are replaced with returned synchronous or asynchronous value",
276+
"properties": {
277+
"$name": {
278+
"$ref": "#"
279+
},
280+
"$args": {
281+
"$ref": "#"
282+
}
283+
}
284+
},
285+
{
286+
"title": "object to [async] value",
287+
"description": "Merge object properties into a single [asynchronous] object value",
288+
"objectToAsync": true
289+
},
290+
{
291+
"title": "execute instruction",
292+
"description": "executes supported script instructions",
293+
"validateAsync": {
294+
"allOf": [
295+
{
296+
"description": "valdate evaluated instruction keywords",
297+
"properties": {
298+
"$name": {
299+
"type": "string",
300+
"anyOf": [
301+
{
302+
"pattern": "^[A-Za-z_$][A-Za-z_$0-9]+$"
303+
},
304+
{
305+
"format": "uuid"
306+
}
307+
]
308+
},
309+
"$args": {
310+
"type": "array",
311+
"minItems": 1,
312+
"items": {
313+
"anyOf": [
314+
{
315+
"type": "string",
316+
"pattern": "^[A-Za-z_$][A-Za-z_$0-9]+$"
317+
},
318+
{
319+
"type": "object",
320+
"minProperties": 1,
321+
"maxProperties": 1,
322+
"patternProperties": {
323+
"^[A-Za-z_$][A-Za-z_$0-9]+$": {
324+
"$ref": "http://json-schema.org/draft-04/schema#"
325+
}
326+
}
327+
}
328+
]
329+
}
330+
}
331+
}
332+
},
333+
{
334+
"description": "execute instruction using custom keyword",
335+
"eval$func": true
336+
}
337+
]
338+
}
339+
}
340+
]
341+
}
342+
},
264343
{
265344
"then": {
266345
"title": "parallel execution",

schema/schema.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
{
2020
"$ref": "#$delay"
2121
},
22+
{
23+
"$ref": "#$func"
24+
},
2225
{
2326
"$ref": "#parallel"
2427
},
@@ -121,6 +124,27 @@
121124
"$delay"
122125
]
123126
},
127+
"_$func": {
128+
"id": "#$func",
129+
"title": "$func",
130+
"description": "function definition",
131+
"type": "object",
132+
"properties": {
133+
"$func": {
134+
"$ref": "#"
135+
},
136+
"$name": {
137+
"$ref": "#"
138+
},
139+
"$args": {
140+
"$ref": "#"
141+
}
142+
},
143+
"additionalProperties": false,
144+
"required": [
145+
"$func"
146+
]
147+
},
124148
"parallel": {
125149
"id": "#parallel",
126150
"description": "scripts in the object are executed in parallel, property names should not start with $",

schema/schema_strict.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
{
2020
"$ref": "#$delay"
2121
},
22+
{
23+
"$ref": "#$func"
24+
},
2225
{
2326
"$ref": "#parallel"
2427
},
@@ -242,6 +245,52 @@
242245
"$delay"
243246
]
244247
},
248+
"_$func": {
249+
"id": "#$func",
250+
"title": "$func",
251+
"description": "function definition",
252+
"type": "object",
253+
"properties": {
254+
"$func": {
255+
"$ref": "#"
256+
},
257+
"$name": {
258+
"anyOf": [
259+
{
260+
"type": "string",
261+
"anyOf": [
262+
{
263+
"pattern": "^[A-Za-z_$][A-Za-z_$0-9]+$"
264+
},
265+
{
266+
"format": "uuid"
267+
}
268+
]
269+
},
270+
{
271+
"allOf": [
272+
{
273+
"type": [
274+
"object",
275+
"array"
276+
]
277+
},
278+
{
279+
"$ref": "#"
280+
}
281+
]
282+
}
283+
]
284+
},
285+
"$args": {
286+
"$ref": "#"
287+
}
288+
},
289+
"additionalProperties": false,
290+
"required": [
291+
"$func"
292+
]
293+
},
245294
"parallel": {
246295
"id": "#parallel",
247296
"description": "scripts in the object are executed in parallel, property names should not start with $",

spec/scripts/$func.json

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
[
2+
{
3+
"description": "$func - function definition",
4+
"schemas": [
5+
{ "$ref": "http://www.json-script.com/schema/schema.json#" },
6+
{ "$ref": "http://www.json-script.com/schema/schema.json#$func" },
7+
{ "$ref": "http://www.json-script.com/schema/schema_strict.json#" },
8+
{ "$ref": "http://www.json-script.com/schema/schema_strict.json#$func" }
9+
],
10+
"tests": [
11+
{
12+
"description": "anonymous function, no arguments definition",
13+
"data": {
14+
"$func": {
15+
"$exec": "myobject",
16+
"$method": "doit",
17+
"$args": [ { "$data": "/0" }, { "$data": "/1" } ]
18+
}
19+
},
20+
"valid": true
21+
},
22+
{
23+
"description": "anonymous function with named arguments",
24+
"data": {
25+
"$func": {
26+
"$exec": "myobject",
27+
"$method": "doit",
28+
"$args": [ { "$data": "/x" }, { "$data": "/y" } ]
29+
},
30+
"$args": ["x", "y"]
31+
},
32+
"valid": true
33+
},
34+
{
35+
"description": "named function with arguments with schemas",
36+
"data": {
37+
"$func": {
38+
"$exec": "myobject",
39+
"$method": "doit",
40+
"$args": [ { "$data": "/x" }, { "$data": "/y" } ]
41+
},
42+
"$name": "doit",
43+
"$args": [
44+
{ "x": { "type": "number" } },
45+
{ "y": { "type": "number" } }
46+
]
47+
},
48+
"valid": true
49+
},
50+
{
51+
"description": "additional propertes are invalid",
52+
"data": {
53+
"$func": {
54+
"$exec": "myobject",
55+
"$method": "doit"
56+
},
57+
"extra": true
58+
},
59+
"valid": false
60+
},
61+
{
62+
"description": "valid if $args is a valid script",
63+
"data": {
64+
"$func": {
65+
"$exec": "myobject",
66+
"$method": "doit"
67+
},
68+
"$args": {
69+
"$exec": "myfunc2",
70+
"$args": [1, 2, 3]
71+
}
72+
},
73+
"valid": true
74+
},
75+
{
76+
"description": "invalid if $args is a invalid script",
77+
"data": {
78+
"$func": {
79+
"$exec": "myobject",
80+
"$method": "doit"
81+
},
82+
"$args": {
83+
"$exec": "myfunc2",
84+
"extra": {}
85+
}
86+
},
87+
"valid": false
88+
}
89+
]
90+
},
91+
{
92+
"description": "$func syntax errors",
93+
"schemas": [
94+
{ "$ref": "http://www.json-script.com/schema/schema.json#" },
95+
{ "$ref": "http://www.json-script.com/schema/schema.json#$func" }
96+
],
97+
"tests": [
98+
{
99+
"description": "$name is scalar but not a string",
100+
"data": {
101+
"$func": {
102+
"$exec": "myobject",
103+
"$method": "doit"
104+
},
105+
"$name": 1
106+
},
107+
"valid": true
108+
},
109+
{
110+
"description": "$name is invalid identifier",
111+
"data": {
112+
"$func": {
113+
"$exec": "myobject",
114+
"$method": "doit"
115+
},
116+
"$name": "foo%bar"
117+
},
118+
"valid": true
119+
}
120+
]
121+
},
122+
{
123+
"description": "$func syntax errors with strict schema",
124+
"schemas": [
125+
{ "$ref": "http://www.json-script.com/schema/schema_strict.json#" },
126+
{ "$ref": "http://www.json-script.com/schema/schema_strict.json#$func" }
127+
],
128+
"tests": [
129+
{
130+
"description": "$name is scalar but not a string",
131+
"data": {
132+
"$func": {
133+
"$exec": "myobject",
134+
"$method": "doit"
135+
},
136+
"$name": 1
137+
},
138+
"valid": false
139+
},
140+
{
141+
"description": "$name is invalid identifier",
142+
"data": {
143+
"$func": {
144+
"$exec": "myobject",
145+
"$method": "doit"
146+
},
147+
"$name": "foo%bar"
148+
},
149+
"valid": false
150+
}
151+
]
152+
}
153+
]

0 commit comments

Comments
 (0)