1- import { JSONValue } from "." ;
2-
31/**
42 * Creates a fake based on the given schema
53 * https://mokapi.io/docs/javascript-api/mokapi-faker/fake
@@ -12,26 +10,298 @@ import { JSONValue } from ".";
1210 * console.log(fake({type: 'string', pattern: '^\d{3}-\d{2}-\d{4}$'})) // 123-45-6789
1311 * }
1412 */
15- export function fake ( schema : Schema ) : JSONValue ;
13+ export function fake ( schema : Schema | JSONSchema ) : any ;
14+
15+ /**
16+ * Gets the tree node with the given name
17+ * @param name name - tree node's name
18+ * @example
19+ * export default function() {
20+ * const root = findByName(RootName)
21+ * root.insert(0, { name: 'foo', test: () => { return true }, fake: () => { return 'foobar' } })
22+ * console.log(fake({type: 'string'}))
23+ * }
24+ */
25+ export function findByName ( name : string ) : Tree ;
26+
27+ /**
28+ * The name of the root faker tree
29+ */
30+ export const RootName = "Faker" ;
31+
32+ /**
33+ * The Tree object represents a node in the faker tree
34+ */
35+ export interface Tree {
36+ /**
37+ * Gets the name of the tree node
38+ */
39+ name : string ;
40+
41+ /**
42+ * Inserts a Tree objects after the last child of this tree.
43+ * @param node node - A Tree node to insert after the last child.
44+ */
45+ append : ( node : Tree | CustomTree ) => void ;
46+
47+ /**
48+ * Inserts a Tree objects at a specified index position
49+ * @param index index - The zero-based index position of the insertion.
50+ * @param node node - The tree node to insert
51+ */
52+ insert : ( index : number , node : Tree | CustomTree ) => void ;
53+
54+ /**
55+ * Removes a Tree node at the specific index position
56+ * @param index index - The zero-based index position to remove.
57+ */
58+ removeAt : ( index : number ) => void ;
59+
60+ /**
61+ * Removes a Tree node with the given name
62+ * @param name name - The name of a node to remove
63+ */
64+ remove : ( name : string ) => void ;
65+ }
66+
67+ /**
68+ * The CustomTree object represents a custom node in the faker tree
69+ */
70+ export interface CustomTree {
71+ /**
72+ * Gets the name of the custom tree node
73+ */
74+ name : string ;
75+
76+ /**
77+ * Tests whether the tree node supports the request.
78+ * @param request request - Request for a new fake value
79+ * @example
80+ * export default function() {
81+ * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
82+ * const node = findByName('Strings')
83+ * node.append({
84+ * name: 'Frequency',
85+ * test: (r) => { return r.lastName() === 'frequency' },
86+ * fake: (r) => {
87+ * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
88+ * }
89+ * })
90+ * return fake({ type: 'string' })
91+ * }
92+ */
93+ test : ( r : Request ) => boolean ;
94+
95+ /**
96+ * Gets a new fake value
97+ * @param request request - Request for a new fake value
98+ * @example
99+ * export default function() {
100+ * const frequencyItems = ['never', 'daily', 'weekly', 'monthly', 'yearly']
101+ * const node = findByName('Strings')
102+ * node.append({
103+ * name: 'Frequency',
104+ * test: (r) => { return r.lastName() === 'frequency' },
105+ * fake: (r) => {
106+ * return frequencyItems[Math.floor(Math.random()*frequencyItems.length)]
107+ * }
108+ * })
109+ * return fake({ type: 'string' })
110+ * }
111+ */
112+ fake : ( r : Request ) => any ;
113+ }
114+
115+ export interface Request {
116+ path : PathElement [ ] ;
117+
118+ last : ( ) => PathElement ;
119+ lastName : ( ) => string ;
120+ lastSchema : ( ) => JSONSchema ;
121+ }
122+
123+ export interface PathElement {
124+ name : string ;
125+ schema : JSONSchema ;
126+ }
127+
128+ /**
129+ * JSON Schema defines a JSON-based format for describing the structure of JSON data
130+ * @example
131+ * {
132+ * "type": "string",
133+ * "format": "email"
134+ * }
135+ */
136+ export interface JSONSchema {
137+ /**
138+ * Specifies the data type for a schema.
139+ */
140+ type ?: SchemaType | SchemaType [ ] ;
141+
142+ /**
143+ * The enum keyword is used to restrict a value to a fixed set of values.
144+ */
145+ enum ?: any [ ] ;
146+
147+ /**
148+ * The const keyword is used to restrict a value to a single value.
149+ */
150+ const ?: any ;
151+
152+ /**
153+ * Contains a list of valid examples.
154+ */
155+ examples ?: any [ ] ;
156+
157+ /**
158+ * Specifies a default value.
159+ */
160+ default ?: any ;
161+
162+ // Numbers
163+ /**
164+ * Restricts the number to a multiple of the given number
165+ */
166+ multipleOf ?: number ;
167+
168+ /**
169+ * Restricts the number to a maximum number
170+ */
171+ maximum ?: number ;
172+
173+ /**
174+ * Restricts the number to a exclusive maximum number
175+ */
176+ exclusiveMaximum ?: number ;
177+
178+ /**
179+ * Restricts the number to a minimum number
180+ */
181+ minimum ?: number ;
182+
183+ /**
184+ * Restricts the number to a exclusive minimum number
185+ */
186+ exclusiveMinimum ?: number ;
187+
188+ // Strings
189+ /**
190+ * Restricts the string to a maximum length
191+ */
192+ maxLength ?: number ;
193+
194+ /**
195+ * Restricts the string to a minimum length
196+ */
197+ minLength ?: number ;
198+
199+ /**
200+ * The pattern keyword is used to restrict a string to a particular regular expression.
201+ */
202+ pattern ?: string ;
203+
204+ /**
205+ * The format keyword allows for basic semantic identification of certain kinds of string values that are commonly used.
206+ */
207+ format ?: string ;
208+
209+ // Arrays
210+ /**
211+ * Specifies the schema of the items in the array.
212+ */
213+ items ?: JSONSchema ;
214+
215+ /**
216+ * Restricts the array to have a maximum length
217+ */
218+ maxItems ?: number ;
219+
220+ /**
221+ * Restricts the array to have a minimum length
222+ */
223+ minItems ?: number ;
224+
225+ /**
226+ * Restricts the array to have unique items
227+ */
228+ uniqueItems ?: boolean ;
229+
230+ // Objects
231+ /**
232+ * Specifies the properties of an object
233+ */
234+ properties ?: { [ name : string ] : JSONSchema } ;
235+
236+ /**
237+ * Restricts the object to have a maximum of properties
238+ */
239+ maxProperties ?: number ;
240+
241+ /**
242+ * Restricts the object to have a minimum of properties
243+ */
244+ minProperties ?: number ;
245+
246+ /**
247+ * Specifies the required properties for an object
248+ */
249+ required ?: string [ ] ;
250+
251+ /**
252+ * The additionalProperties keyword is used to control the handling of extra stuff,
253+ * that is, properties whose names are not listed in the properties keyword or match
254+ * any of the regular expressions in the patternProperties keyword. By default, any
255+ * additional properties are allowed.
256+ */
257+ additionalProperties ?: boolean | JSONSchema ;
258+
259+ /**
260+ * A value must be valid against all the schemas
261+ */
262+ allOf ?: JSONSchema [ ] ;
263+
264+ /**
265+ * A value must be valid against any the schemas
266+ */
267+ anyOf ?: JSONSchema [ ] ;
268+
269+ /**
270+ * A value must be valid against exactly one the schemas
271+ */
272+ oneOf ?: JSONSchema [ ] ;
273+ }
274+
275+ export type SchemaType = "object" | "array" | "number" | "integer" | "string" | "boolean" | "null" ;
16276
17277export interface Schema {
18278 /** Type of fake value */
19- type ?: "object" | "array" | "number" | "integer" | "string" | "boolean" ;
279+ type ?: SchemaType | SchemaType [ ] ;
20280
21281 /** Serves as a hint at the contents and format of the string. */
22282 format ?: string ;
23283
24284 /** Specifies regular expression that fake must match. */
25285 pattern ?: string ;
26286
287+ /**
288+ * Restricts the string to a minimum length
289+ */
290+ minLength ?: number ;
291+
292+ /**
293+ * Restricts the string to a maximum length
294+ */
295+ maxLength ?: number ;
296+
27297 /** Describes the type and format of array items. */
28298 items ?: Schema ;
29299
30300 /** Specifies a list of required properties. */
31301 required ?: string [ ] ;
32302
33303 /** Specify possible value for this schema. */
34- enum ?: JSONValue [ ] ;
304+ enum ?: any [ ] ;
35305
36306 /** Specifies the minimum range of possible values. */
37307 minimum ?: number ;
@@ -40,10 +310,23 @@ export interface Schema {
40310 maximum ?: number ;
41311
42312 /** Specifies whether minimum value is exluded. Default is false. */
43- exclusiveMinimum ?: boolean ;
313+ exclusiveMinimum ?: number | boolean ;
44314
45315 /** ** Specifies whether maximum value is exluded. Default is false. */
46- exclusiveMaximum ?: boolean ;
316+ exclusiveMaximum ?: number | boolean ;
317+
318+ /**
319+ * Specifies the properties of an object
320+ */
321+ properties ?: { [ name : string ] : Schema } ;
322+
323+ /**
324+ * The additionalProperties keyword is used to control the handling of extra stuff,
325+ * that is, properties whose names are not listed in the properties keyword or match
326+ * any of the regular expressions in the patternProperties keyword. By default, any
327+ * additional properties are allowed.
328+ */
329+ additionalProperties ?: boolean | Schema | undefined ;
47330
48331 /** Valid against one of the specified schemas. */
49332 anyOf ?: Schema [ ] ;
0 commit comments