forked from swagger-api/swagger-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.js
56 lines (55 loc) · 1.95 KB
/
schemas.js
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
export const validate2And3TypeArrayRequiresItems = () => (system) => {
return system.validateSelectors
.allSchemas()
.then(nodes => {
return nodes.reduce((acc, node) => {
const schemaObj = node.node
const { type, items } = schemaObj || {}
if(type === "array" && typeof items === "undefined") {
acc.push({
message: "Schemas with 'type: array', require a sibling 'items: ' field",
path: node.path,
level: "error",
})
} else if(type === "array" && (typeof items !== "object" || Array.isArray(items))) {
acc.push({
message: "`items` must be an object",
path: [...node.path, "items"],
level: "error",
})
}
return acc
}, [])
})
}
export const validate2And3TypesInDefaultValuesMatchesWithEnum = () => (system) => {
return system.validateSelectors
.allSchemas()
.then(nodes => {
return nodes.reduce((acc, node) => {
const schemaObj = node.node
const { type, items } = schemaObj || {}
const enumeration = schemaObj.enum
var isValidFormat = true
enumeration.forEach(element => {
if (type === "array" && (!Array.isArray(element) || element === null)) {
isValidFormat = false
} else if ((type === "number" || type === "string" || type === "boolean") && !(typeof element === type)) {
isValidFormat = false
} else if (type === "integer" && !Number.isInteger(element)) {
isValidFormat = false
} else if (type === "object" && ((element == null) || !(typeof element === type))) {
isValidFormat = false
}
});
if (!isValidFormat) {
acc.push({
message: "enum value should conform to its schema's `type`",
path: [...node.path, "enum"],
level: "warning",
});
}
return acc
}, [])
})
}