presidium 1.4.10 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OptionalValidator.js +5 -5
- package/StrictValidator.js +13 -15
- package/package.json +1 -1
package/OptionalValidator.js
CHANGED
|
@@ -66,11 +66,11 @@ const OptionalValidator = function (parsers) {
|
|
|
66
66
|
|
|
67
67
|
return function optionalValidator(object) {
|
|
68
68
|
const result = {}
|
|
69
|
-
for (const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
result[
|
|
69
|
+
for (const field in parsers) {
|
|
70
|
+
if (field in object) {
|
|
71
|
+
const parser = parsers[field]
|
|
72
|
+
const value = object[field]
|
|
73
|
+
result[field] = parser(value)
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
return result
|
package/StrictValidator.js
CHANGED
|
@@ -62,30 +62,28 @@
|
|
|
62
62
|
*/
|
|
63
63
|
const StrictValidator = function (parsers, options = {}) {
|
|
64
64
|
const {
|
|
65
|
-
onMissing
|
|
66
|
-
const error = new Error(`missing field ${field}`)
|
|
67
|
-
error.code = 400
|
|
68
|
-
throw error
|
|
69
|
-
},
|
|
65
|
+
onMissing
|
|
70
66
|
} = options
|
|
71
67
|
|
|
72
68
|
const requiredFields = Object.keys(parsers)
|
|
73
69
|
|
|
74
70
|
return function strictValidator(object) {
|
|
75
|
-
|
|
76
|
-
|
|
71
|
+
const result = {}
|
|
72
|
+
for (const field in parsers) {
|
|
73
|
+
if (field in object) {
|
|
74
|
+
const parser = parsers[field]
|
|
75
|
+
const value = object[field]
|
|
76
|
+
result[field] = parser(value)
|
|
77
|
+
|
|
78
|
+
} else if (typeof onMissing == 'function') {
|
|
77
79
|
onMissing(field)
|
|
80
|
+
} else {
|
|
81
|
+
const error = new Error(`Missing field ${field}`)
|
|
82
|
+
error.code = 400
|
|
83
|
+
throw error
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
const result = {}
|
|
82
|
-
for (const key in parsers) {
|
|
83
|
-
const parser = parsers[key],
|
|
84
|
-
value = object[key]
|
|
85
|
-
if (value != null) {
|
|
86
|
-
result[key] = parser(value)
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
87
|
return result
|
|
90
88
|
}
|
|
91
89
|
}
|