-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (71 loc) · 1.99 KB
/
Copy pathutils.js
File metadata and controls
81 lines (71 loc) · 1.99 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
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
import selectn from "./selectn";
export function normRef(ref) {
return ref.replace(/\$/g, ".");
}
export function selectRef(field, formData) {
let ref = normRef(field);
return selectn(ref, formData);
}
export function isObject(obj) {
return typeof obj === "object" && obj !== null;
}
export function isDevelopment() {
return process.env.NODE_ENV !== "production";
}
export function toArray(event) {
if (Array.isArray(event)) {
return event;
} else {
return [event];
}
}
export function toError(message) {
if (isDevelopment()) {
throw new ReferenceError(message);
} else {
console.error(message);
}
}
export function isRefArray(field, schema) {
return (
schema.properties[field] &&
schema.properties[field].type === "array" &&
schema.properties[field].items &&
schema.properties[field].items["$ref"]
);
}
function fetchSchema(ref, schema) {
if (ref.startsWith("#/")) {
return ref
.substr(2)
.split("/")
.reduce((schema, field) => schema[field], schema);
} else {
toError(
"Only local references supported at this point use json-schema-deref"
);
return undefined;
}
}
export function extractRefSchema(field, schema) {
let { properties } = schema;
if (!properties || !properties[field]) {
toError(`${field} not defined in properties`);
return undefined;
} else if (properties[field].type === "array") {
if (isRefArray(field, schema)) {
return fetchSchema(properties[field].items["$ref"], schema);
} else {
return properties[field].items;
}
} else if (properties[field] && properties[field]["$ref"]) {
return fetchSchema(properties[field]["$ref"], schema);
} else if (properties[field] && properties[field].type === "object") {
return properties[field];
} else {
toError(`${field} has no $ref field ref schema extraction is impossible`);
return undefined;
}
}
const concat = (x, y) => x.concat(y);
export const flatMap = (xs, f) => xs.map(f).reduce(concat, []);