-
Notifications
You must be signed in to change notification settings - Fork 10
/
parameter-value.js
164 lines (141 loc) · 5.35 KB
/
parameter-value.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const _ = require('lodash')
const JsonRefs = require('json-refs')
const ZSchema = require('./z-schema')
const helpers = require('./helpers')
const validator = new ZSchema({
breakOnFirstError: false,
ignoreUnknownFormats: true,
reportPathAsArray: true,
assumeAdditional: true
})
class ParameterValue {
constructor (parameterObject, raw) {
let pPath = JsonRefs.pathFromPtr(parameterObject.ptr)
let processed = false
let schema = parameterObject.schema
let error
let isValid
let processedValue
this.parameterObject = parameterObject
this.raw = raw
// Use Object.defineProperty for 'value' to allow for lazy processing of the raw value
Object.defineProperties(this, {
error: {
enumerable: true,
get: function () {
// Always call this.valid to ensure we validate the value prior to returning any values
if (this.valid === true) {
return undefined
}
return error
}
},
valid: {
enumerable: true,
get: function () {
let result = {
errors: [],
warnings: []
}
let skipValidation = false
let value
let vError
if (_.isUndefined(isValid)) {
isValid = true
value = this.value
if (_.isUndefined(error)) {
try {
// Validate requiredness
if (parameterObject.required === true && _.isUndefined(value)) {
vError = new Error('Value is required but was not provided')
vError.code = 'REQUIRED'
throw vError
}
// Cases we do not want to do schema validation:
//
// * The schema explicitly allows empty values and the value is empty
// * The schema allow optional values and the value is undefined
// * The schema defines a file parameter
// * The schema is for a string type with date/date-time format and the value is a date
// * The schema is for a string type and the value is a Buffer
if ((_.isUndefined(parameterObject.required) || parameterObject.required === false) &&
_.isUndefined(value)) {
skipValidation = true
} else if (schema.allowEmptyValue === true && value === '') {
skipValidation = true
} else if (parameterObject.type === 'file') {
skipValidation = true
} else if (schema.type === 'string') {
if (['date', 'date-time'].indexOf(schema.format) > -1 && _.isDate(value)) {
skipValidation = true
} else if (schema.type === 'string' && _.isFunction(value.readUInt8)) {
skipValidation = true
}
}
if (!skipValidation) {
// Validate against JSON Schema
result = helpers.validateAgainstSchema(validator, parameterObject.schema, value)
}
if (result.errors.length > 0) {
vError = new Error('Value failed JSON Schema validation')
vError.code = 'SCHEMA_VALIDATION_FAILED'
vError.errors = result.errors
throw vError
}
} catch (err) {
err.failedValidation = true
err.path = pPath
error = err
isValid = false
}
} else {
isValid = false
}
}
return isValid
}
},
value: {
enumerable: true,
get: function () {
if (!processed) {
if (schema.type === 'file') {
processedValue = raw
} else {
// Convert/Coerce the raw value from the request object
try {
processedValue = helpers.convertValue(schema, {
collectionFormat: parameterObject.collectionFormat
}, raw)
} catch (err) {
error = err
}
// If there is still no value and there are no errors, use the default value if available (no coercion)
if (_.isUndefined(processedValue) && _.isUndefined(error)) {
if (schema.type === 'array') {
if (_.isArray(schema.items)) {
processedValue = _.reduce(schema.items, function (items, item) {
items.push(item.default)
return items
}, [])
// If none of the items have a default value reset the processed value to 'undefined'
if (_.every(processedValue, _.isUndefined)) {
processedValue = undefined
}
} else if (!_.isUndefined(schema.items) && !_.isUndefined(schema.items.default)) {
processedValue = [schema.items.default]
}
} else if (!_.isUndefined(schema.default)) {
processedValue = schema.default
}
}
}
processed = true
}
return processedValue
}
}
})
}
}
module.exports = ParameterValue