-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjson-schema-faker.node.txt
185 lines (161 loc) · 12.2 KB
/
json-schema-faker.node.txt
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
┏━━━━━━━━━━━━━━━━━━━━━━━┓
┃ JSON-SCHEMA-FAKER ┃
┗━━━━━━━━━━━━━━━━━━━━━━━┛
ALTERNATIVES ==> # - json-schema-faker (preferred):
# - JSON schema v5 + const
# - custom functions, including easy access to faker.js, chance.js, randexp
# - JSON paths
# - auto-incremental number|dates
# - custom source of randomness
# - mocker-data-generator:
# - good support for relations
# - const, enum, min|maxItems|Properties
# - easy access to faker.js, chance.js, casual.js, randexp
# - sibling properties
# - custom functions
# - auto-incremental number
VERSION ==> #0.5.0-rc23
#Generates fake data based on JSON schema
#Node/browser
┌──────────┐
│ CORE │
└──────────┘
JSF.resolve #Generate fake data
(JSON_SCHEMA[, OBJ_ARR][, 'DIR'])#Resolves JSON references with JSON-SCHEMA-REF-PARSER.dereference('DIR') (see its doc)
->VAL # - uses POPTS.dereference.circular 'ignore'
# - can add JSON references as 'NAME' using OBJ_ARR: id 'NAME', ...
# - 'DIR' (def: process.cwd()) is root for relative references
JSF.generate #Same but without calling JSON-SCHEMA-REF-PARSER
(JSON_SCHEMA[, OBJ_ARR])->VAL #Can still use OBJ_ARR JSON references
#Can use OPTS.ignoreMissingRefs BOOL (def: false)
JSF.option(OPTS) #
JSF.option('NAME')->VAL #
JSF.option('NAME', VAL) #
JSF.option.getDefaults()->OBJ #
┌────────────────┐
│ GENERATION │
└────────────────┘
JSON SCHEMA VERSION ==> #Full v5 is supported except:
# - "not" with: enum (bug), format|multipleOf|pattern|items|additionalItems|min|maxItems|
# uniqueItems|min|maxProperties|properties|additionalProperties|patternProperties
# - "dependencies": noop
#Also v6:
# - const, $id, examples
#Formats full v7 + ajv-specific except:
# - v7: regex, date, time, relative-json-pointer
# - ajv: url, json-pointer-uri-fragment
JSON_SCHEMA.type #If:
# - missing: guessed from sibling properties
# - invalid:
# - throws unless OPTS.failOnInvalidTypes false
# - in which case, must set which value to generate with OPTS.defaultInvalidTypeProduct VAL
#Behavior:
# - string: random latin words
# - number|integer: between minimum and maximum
# - boolean: random pick
# - object|array: according to items, properties, etc.
# - several types (ARR): random pick
JSON_SCHEMA.format #Formats:
# - email, hostname, ipv6, uri[-reference]: use RANDEXP on related regexps
# - ipv4: random numbers
# - date-time, date, time
#If not supported, throws unless OPTS.failOnInvalidFormat false
#Can add custom ones:
# - JSF.format('FORMAT', FUNC(JSON_SCHEMA)->VAL)
# - JSF.format('FORMAT', null)
# - JSF.format('FORMAT')->FUNC
# - JSF.format({ FORMAT: FUNC, ... })
# - JSF.format()->OBJ
JSON_SCHEMA.enum #Random pick
JSON_SCHEMA.const #Set as is
JSON_SCHEMA.default #Used if OPTS.useDefaultValue true (def: false)
JSON_SCHEMA.examples #Random pick if OPTS.useExamplesValue true (def: false)
JSON_SCHEMA.multipleOf #
JSON_SCHEMA. #Random number between both
[exclusive]minimum|maximum #Def: -1e8, 1e8
JSON_SCHEMA.minLength|maxLength #Random number between both (def: 0, 140)
#Can set default with OPTS.minLength|maxLength
# - if both are present, take the largest|smallest one
JSON_SCHEMA.pattern #Use JSF.random.randexp()
JSON_SCHEMA.items #items ARR is supported
JSON_SCHEMA.additionalItems #
JSON_SCHEMA.minItems|maxItems #Random number between both (def: 1, 5)
#Can set default with OPTS.minItems|maxItems
# - if both are present, take the largest|smallest one
#If both OPTS.optionalsProbability and maxItems defined:
# - maxLength = maxItems * optionalsProbability
# - if OPTS.fixedProbabilities true (def: false): length = maxLength (i.e. minItems is ignored)
JSON_SCHEMA.uniqueItems #
JSON_SCHEMA.min|maxProperties #Random number between both (def: number required props, number props)
JSON_SCHEMA.required #OPTS.optionalsProbability NUM (def: 0):
# - how likely non-required properties will generate a value
# - OPTS.alwaysFakeOptionals BOOL (def: false): same as OPTS.optionalsProbability 1 + OPTS.fixedProbabilities true
# - OPTS.requiredOnly BOOL (def: false): same as OPTS.optionalsProbability 0
# - OPTS.fixedProbabilities true (def: false): always use highest possible number
JSON_SCHEMA.properties #OPTS.ignoreProperties PROP_REGEXP|STR[()]_ARR (def: []):
# - do not generate for those properties
# - if OPTS.fillProperties true (def), they do not count towards minProperties|maxProperties
JSON_SCHEMA.additionalProperties #Key is random latin word
# - if OPTS.reuseProperties true (def: false), use existing properties names instead,
# with number as suffix
#Only generated if JSON_SCHEMA.minProperties
JSON_SCHEMA.patternProperties #Generate prop name with RANDEXP
#Only generated if OPTS.optionalsProbability >= 0.5
JSON_SCHEMA.dependencies #
JSON_SCHEMA.not #Inverse type|[exclusive]minimum|maximum|minLength|maxLength|required
JSON_SCHEMA.allOf #Deep merged schemas (including array which are intersected)
JSON_SCHEMA.anyOf|oneOf #Random pick
┌──────────────────┐
│ NON-STANDARD │
└──────────────────┘
JSON_SCHEMA.autoIncrement #true|OPTS:
# - initial number is JSON_SCHEMA.offset or JSON_SCHEMA|OPTS.initialOffset
# - def: from JSON_SCHEMA.minimum (def: 1) to minimum + 100
JSON_SCHEMA.sequentialDate #true|STR:
# - incremental date, as 'DATE'
# - initial date is random (using JSF.random.date())
# - increment is random (using JSF.random.date(STR), def: 'days')
JSON_SCHEMA.jsonPath #Resolves a JSON path (using dchester jsonpath library), from root input
#Only if OPTS.resolveJsonPath true (def: false)
#OBJ:
# - path 'JSON_PATH'
# - including '$..*' for siblings
# - count NUM (def: 1):
# - if 1 (def), returns first matching element
# - if > 1, picks NUM and returns random one
# - group STR (def: property name):
# - two identical JSON_PATHs are independent from each other
# - unless same group, in which case they get same value
# - if count > 1 and cycle|reverse true, returns next|previous value instead
# - cycle BOOL (def: false), reverse BOOL (def: false): see above
#Can use 'JSON_PATH' as shortcut for { path: 'JSON_PATH' }
┌────────────┐
│ CUSTOM │
└────────────┘
JSF.define #Add JSON_SCHEMA.[x-]PROP: VAL, which will generate VAL2
('PROP', FUNC(VAL, JSON_SCHEMA, #PARENT_OBJ is generated parent object. It might be only partially built though (according to keys order)
'ATTR', PARENT_OBJ)->VAL2) #All siblings receive same context `this` (empty object)
JSF.extend #Add JSON_SCHEMA.[x-]PROP: 'VARR' which will generate OBJ.VARR[()]
('PROP', FUNC([FUNC2])->OBJ) #Can also JSON_SCHEMA.[x-]PROP: { VARR: VAL[_ARR] } which will generate OBJ.VARR[(VAL,...)]
# - VAL can use #{VAR} resolved to PARENT_OBJ.VAR
# - if VAL is an array, apply template on each element
#FUNC2 is formerly registered 'PROP' (if JSF.extend('PROP', FUNC2) was already called)
#Often used: faker.js, chance.js
JSF.locate('PROP')->OBJ #Returns OBJ registered by JSF.extend('PROP')
JSF.reset(['PROP']) #Remove from JSF.define|extend()
┌─────────────┐
│ HELPERS │
└─────────────┘
OPTS.random()->NUM #Source of randomness (def: Math.random())
#All helpers use it too.
JSF.random.pick(ARR)->VAL #Returns random element
JSF.random.shuffle(ARR)->ARR #
JSF.random.number([MIN][, MAX]
[, DEF_MIN][, DEF_MAX][, BOOL]) #Returns NUM between MIN (def: DEF_MIN, def: -100) and MAX (def: DEF_MAX, def: 100)
->NUM #If BOOL true (def: false), returns integer.
JSF.random.date([STR])->DATE #Returns random DATE between 1938 and now
#If STR 'seconds|minutes|hours|days|weeks|months|years', returns number of ms for few 'seconds|...'
JSF.random.randexp(REGEXP[_STR]) #Returns random STR matching REGEXP
->STR #Uses Randexp.randexp() (see its doc):
# - REGEXP.max is OPTS.defaultRandExpMax (def: 10)
# - REGEXP.randInt() uses OPTS.random()