This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
164 lines (133 loc) · 3.99 KB
/
index.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
var _ = require('lodash')
var types = require('./types')
var introspect = require('introspect')
var ERRORS = exports.ERRORS = {
MISSING_REQUIRED_FIELD: 'missing required field',
INVALID_VALUE: 'invalid value'
}
var PLATFORM = {
POSTGRES: 1,
REDSHIFT: 2
}
Object.keys(PLATFORM).forEach(function(k){
exports[k] = PLATFORM[k]
})
// Shortcuts
exports.redshift = {
object: function(obj, metadata) {
return exports.object(obj, metadata, { platform: PLATFORM.REDSHIFT })
},
validatorFor: function(fieldMetadata) {
return validatorFor(fieldMetadata, PLATFORM.REDSHIFT)
}
}
exports.pg = {
object: function(obj, metadata) {
return exports.object(obj, metadata, { platform: PLATFORM.POSTGRES })
},
validatorFor: function(fieldMetadata) {
return validatorFor(fieldMetadata, PLATFORM.POSTGRES)
}
}
exports.object = function (obj, metadata, opts) {
var errors = []
var platform = opts && opts.platform
_.forEach(metadata, function (fieldMetadata, field) {
var value = obj[field]
// we might miss zeros here, so
if (value === undefined || value === null) {
if (fieldMetadata.required) {
errors.push({
field: field,
error: ERRORS.MISSING_REQUIRED_FIELD
})
}
return
}
var validator = validatorFor(fieldMetadata, platform)
if (!validator.isValidValue(value)) {
errors.push({
field: field,
error: ERRORS.INVALID_VALUE
})
}
})
return errors
}
function validatorFor (fieldMetadata, platform) {
if (!platform) platform = PLATFORM.POSTGRES
var type = fieldMetadata.type
var validators = platformValidators(platform)
if (type in validators) {
var validator = validators[type]
if (typeof validator === 'function') {
// Has user-specified type options(s)
var options = _.values(_.pick(fieldMetadata, introspect(validator)))
validator = validator.apply(null, options)
}
return validator
}
throw new Error('missing validator for type ' + type)
}
/*
these are validators that can be reused
*/
var staticValidators = {}
var platformAgnostic = {
boolean: new types.Boolean(),
char: types.Char,
int2: new types.Integer('16bit'),
int4: new types.Integer('32bit'),
int8: new types.Integer('64bit'),
serial: new types.Integer('serial'),
bigserial: new types.Integer('bigserial'),
interval: new types.Interval
}
var ALIAS = {
smallint: 'int2',
integer: 'int4',
bigint: 'int8',
varchar: 'char',
numeric: 'decimal',
float4: 'real',
float8: 'double_precision',
timestamptz: 'timestamp',
timetz: 'time',
bpchar: 'char',
jsonb: 'json'
}
function platformValidators(platform) {
if (staticValidators[platform]) return staticValidators[platform]
var validators = staticValidators[platform] = _.assign({}, platformAgnostic)
if (platform === PLATFORM.POSTGRES) {
var platformTypes = types.postgres
} else if (platform === PLATFORM.REDSHIFT) {
platformTypes = types.redshift
} else {
throw new Error('Invalid platform: ' + platform)
}
validators.decimal = platformTypes.Decimal
validators.json = platformTypes.JSON
validators.text = platformTypes.Text
validators.uuid = platformTypes.UUID
validators.date = new platformTypes.Date
validators.time = new platformTypes.Time
validators.timestamp = new platformTypes.Timestamp
// Inexact. Maximum precision is advisory and *at least* 6.
// Using the PostgreSQL type for Redshift too, because Redshift's
// limits on decimal precision shouldn't be applied to inexact types.
validators.real = new types.postgres.Decimal(null, null, '128bit')
// Inexact. Maximum precision is advisory and *at least* 15.
validators.double_precision = new types.postgres.Decimal(null, null, '1024bit')
if (platform === PLATFORM.POSTGRES) {
validators.float = types.postgres.Float.factory(validators)
} else {
validators.float = validators.double_precision
}
for(var alias in ALIAS) {
validators[alias] = validators[ALIAS[alias]]
}
staticValidators[platform] = validators
return validators
}
exports.validatorFor = validatorFor