-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpatch-properties-correspond-to-put-properties.ts
67 lines (57 loc) · 2.7 KB
/
patch-properties-correspond-to-put-properties.ts
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
// Validates that each patch request body contains one or more properties present in the corresponding put request body,
// and contains only properties present in the put request body.
// RPC Code: RPC-Patch-V1-01
import _ from "lodash"
import { getAllPropertiesIncludingDeeplyNestedProperties } from "./utils"
const ERROR_MESSAGE =
"A patch request body must only contain properties present in the corresponding put request body, and must contain at least one of the properties."
const PARAM_IN_BODY = (paramObject: any) => paramObject.in === "body"
const PATCH = "patch"
const PUT = "put"
const PARAMETERS = "parameters"
export const patchPropertiesCorrespondToPutProperties = (pathItem: any, _opts: any, ctx: any) => {
if (pathItem === null || typeof pathItem !== "object") {
return []
}
const path = ctx.path.concat([PATCH, PARAMETERS])
const errors: any = []
// array of all the patch body param properties
// let patchBodyPropertiesList: any = []
// let putBodyPropertiesList: any = []
const patchBodyProperties: any[] = pathItem[PATCH]?.parameters?.filter(PARAM_IN_BODY).map((param: any) => getAllPropertiesIncludingDeeplyNestedProperties(param.schema,[]))
const putBodyProperties: any[] = pathItem[PUT]?.parameters?.filter(PARAM_IN_BODY).map((param: any) => getAllPropertiesIncludingDeeplyNestedProperties(param.schema,[]))
const patchBodyPropertiesEmpty: boolean = patchBodyProperties.length < 1
const putBodyPropertiesEmpty: boolean = putBodyProperties.length < 1
//patch without at least one body properties => error
if (patchBodyPropertiesEmpty) {
return [
{
message: "Patch operations body cannot be empty.",
path: path,
},
]
}
//patch body nonempty while put body empty => error
if (!patchBodyPropertiesEmpty && putBodyPropertiesEmpty) {
return [
{
message: "Non empty patch body with an empty put body is not valid.",
path: path,
},
]
}
// array of all the patch body properties that are not present in the put body (if any)
//considering only the first element of patchBodyProperties & putBodyProperties is because there will only be one body param
const patchBodyPropertiesNotInPutBody = _.differenceWith(patchBodyProperties[0], putBodyProperties[0], _.isEqual)
// there is at least one property present in the patch body that is not present in the the put body => error
if (patchBodyPropertiesNotInPutBody.length > 0) {
patchBodyPropertiesNotInPutBody.forEach((missingProperty) =>
errors.push({
message: `${Object.keys(missingProperty)[0]} property in patch body is not present in the corresponding put body. ` + ERROR_MESSAGE,
path: path,
})
)
return errors
}
return []
}