forked from stoplightio/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.ts
More file actions
37 lines (32 loc) · 1.34 KB
/
extensions.ts
File metadata and controls
37 lines (32 loc) · 1.34 KB
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
import $RefParser from '@stoplight/json-schema-ref-parser';
import { decycle } from '@stoplight/json';
import { get, camelCase, forOwn } from 'lodash';
import { JSONSchemaFaker } from 'json-schema-faker';
import type { JSONSchemaFakerOptions } from 'json-schema-faker';
import { resetJSONSchemaGenerator } from '@stoplight/prism-http';
export async function configureExtensionsUserProvided(
specFilePathOrObject: string | object,
cliParamOptions: { [option: string]: any }
): Promise<void> {
const result = decycle(await new $RefParser().dereference(specFilePathOrObject));
resetJSONSchemaGenerator();
forOwn(get(result, 'x-json-schema-faker', {}), (value: any, option: string) => {
setFakerValue(option, value);
});
// cli parameter takes precidence, so it is set after spec extensions are configed
for (const param in cliParamOptions) {
if (cliParamOptions[param] !== undefined) {
setFakerValue(param, cliParamOptions[param]);
}
}
}
function setFakerValue(option: string, value: any) {
if (option === 'locale') {
// necessary as workaround broken types in json-schema-faker
// @ts-ignore
return JSONSchemaFaker.locate('faker').setLocale(value);
}
// necessary as workaround broken types in json-schema-faker
// @ts-ignore
JSONSchemaFaker.option(camelCase(option) as keyof JSONSchemaFakerOptions, value);
}