-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.js
More file actions
29 lines (25 loc) · 995 Bytes
/
type.js
File metadata and controls
29 lines (25 loc) · 995 Bytes
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
var format = require("./lib/format")
var MESSAGE = "Expected %s to be a %s"
// var types = [
// [String, "string"],
// [Boolean, "boolean"],
// [Object, Type("object")],
// [Number, Type("number")]
// ]
module.exports = type
function type(typeName, message) {
message = message || MESSAGE
return function validate(value, key) {
if (typeName === Number && typeof value !== "number") {
return { message: format(message, key, "number"), type: "type" }
} else if (typeName === String && typeof value !== "string") {
return { message: format(message, key, "string"), type: "type" }
} else if (typeName === Boolean && typeof value !== "boolean") {
return { message: format(message, key, "boolean"), type: "type" }
} else if (typeName === Object &&
(typeof value !== "object" || !value)
) {
return { message: format(message, key, "object"), type: "type" }
}
}
}