Skip to content

Commit 7ff6555

Browse files
fix: add nullable date/time support (#437)
1 parent 5d91d09 commit 7ff6555

File tree

3 files changed

+127
-8
lines changed

3 files changed

+127
-8
lines changed

index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ class Serializer {
134134
}
135135
}
136136

137+
asDatetimeNullable (date, skipQuotes) {
138+
return date === null ? 'null' : this.asDatetime(date, skipQuotes)
139+
}
140+
137141
asDate (date, skipQuotes) {
138142
const quotes = skipQuotes === true ? '' : '"'
139143
if (date instanceof Date) {
@@ -145,6 +149,10 @@ class Serializer {
145149
}
146150
}
147151

152+
asDateNullable (date, skipQuotes) {
153+
return date === null ? 'null' : this.asDate(date, skipQuotes)
154+
}
155+
148156
asTime (date, skipQuotes) {
149157
const quotes = skipQuotes === true ? '' : '"'
150158
if (date instanceof Date) {
@@ -156,6 +164,10 @@ class Serializer {
156164
}
157165
}
158166

167+
asTimeNullable (date, skipQuotes) {
168+
return date === null ? 'null' : this.asTime(date, skipQuotes)
169+
}
170+
159171
asString (str, skipQuotes) {
160172
const quotes = skipQuotes === true ? '' : '"'
161173
if (str instanceof Date) {
@@ -293,15 +305,11 @@ function build (schema, options) {
293305
schema = location.schema
294306
break
295307
case 'string':
296-
if (schema.nullable) {
297-
return serializer.asStringNullable.bind(serializer)
298-
}
299-
300308
switch (schema.format) {
301-
case 'date-time': return serializer.asDatetime.bind(serializer)
302-
case 'date': return serializer.asDate.bind(serializer)
303-
case 'time': return serializer.asTime.bind(serializer)
304-
default: return serializer.asString.bind(serializer)
309+
case 'date-time': return schema.nullable ? serializer.asDatetimeNullable.bind(serializer) : serializer.asDatetime.bind(serializer)
310+
case 'date': return schema.nullable ? serializer.asDateNullable.bind(serializer) : serializer.asDate.bind(serializer)
311+
case 'time': return schema.nullable ? serializer.asTimeNullable.bind(serializer) : serializer.asTime.bind(serializer)
312+
default: return schema.nullable ? serializer.asStringNullable.bind(serializer) : serializer.asString.bind(serializer)
305313
}
306314
case 'integer':
307315
return schema.nullable ? serializer.asIntegerNullable.bind(serializer) : serializer.asInteger.bind(serializer)

test/date.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ test('render a date in a string when format is date-format as ISOString', (t) =>
4040
t.ok(validate(JSON.parse(output)), 'valid schema')
4141
})
4242

43+
test('render a nullable date in a string when format is date-format as ISOString', (t) => {
44+
t.plan(2)
45+
46+
const schema = {
47+
title: 'a date in a string',
48+
type: 'string',
49+
format: 'date-time',
50+
nullable: true
51+
}
52+
const toStringify = new Date()
53+
54+
const validate = validator(schema)
55+
const stringify = build(schema)
56+
const output = stringify(toStringify)
57+
58+
t.equal(output, JSON.stringify(toStringify))
59+
t.ok(validate(JSON.parse(output)), 'valid schema')
60+
})
61+
4362
test('render a date in a string when format is date as YYYY-MM-DD', (t) => {
4463
t.plan(2)
4564

@@ -58,6 +77,25 @@ test('render a date in a string when format is date as YYYY-MM-DD', (t) => {
5877
t.ok(validate(JSON.parse(output)), 'valid schema')
5978
})
6079

80+
test('render a nullable date in a string when format is date as YYYY-MM-DD', (t) => {
81+
t.plan(2)
82+
83+
const schema = {
84+
title: 'a date in a string',
85+
type: 'string',
86+
format: 'date',
87+
nullable: true
88+
}
89+
const toStringify = new Date()
90+
91+
const validate = validator(schema)
92+
const stringify = build(schema)
93+
const output = stringify(toStringify)
94+
95+
t.equal(output, `"${moment(toStringify).format('YYYY-MM-DD')}"`)
96+
t.ok(validate(JSON.parse(output)), 'valid schema')
97+
})
98+
6199
test('verify padding for rendered date in a string when format is date', (t) => {
62100
t.plan(2)
63101

@@ -97,6 +135,28 @@ test('render a date in a string when format is time as kk:mm:ss', (t) => {
97135
t.ok(validate(JSON.parse(output)), 'valid schema')
98136
})
99137

138+
test('render a nullable date in a string when format is time as kk:mm:ss', (t) => {
139+
t.plan(3)
140+
141+
const schema = {
142+
title: 'a date in a string',
143+
type: 'string',
144+
format: 'time',
145+
nullable: true
146+
}
147+
const toStringify = new Date()
148+
149+
const validate = validator(schema)
150+
const stringify = build(schema)
151+
const output = stringify(toStringify)
152+
153+
validate(JSON.parse(output))
154+
t.equal(validate.errors, null)
155+
156+
t.equal(output, `"${moment(toStringify).format('HH:mm:ss')}"`)
157+
t.ok(validate(JSON.parse(output)), 'valid schema')
158+
})
159+
100160
test('render a midnight time', (t) => {
101161
t.plan(3)
102162

test/nullable.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,54 @@ test('handle nullable string correctly', (t) => {
180180
t.same(result, JSON.stringify(data))
181181
t.same(JSON.parse(result), data)
182182
})
183+
184+
test('handle nullable date-time correctly', (t) => {
185+
t.plan(2)
186+
187+
const schema = {
188+
type: 'string',
189+
format: 'date-time',
190+
nullable: true
191+
}
192+
const stringify = build(schema)
193+
194+
const data = null
195+
const result = stringify(data)
196+
197+
t.same(result, JSON.stringify(data))
198+
t.same(JSON.parse(result), data)
199+
})
200+
201+
test('handle nullable date correctly', (t) => {
202+
t.plan(2)
203+
204+
const schema = {
205+
type: 'string',
206+
format: 'date',
207+
nullable: true
208+
}
209+
const stringify = build(schema)
210+
211+
const data = null
212+
const result = stringify(data)
213+
214+
t.same(result, JSON.stringify(data))
215+
t.same(JSON.parse(result), data)
216+
})
217+
218+
test('handle nullable time correctly', (t) => {
219+
t.plan(2)
220+
221+
const schema = {
222+
type: 'string',
223+
format: 'time',
224+
nullable: true
225+
}
226+
const stringify = build(schema)
227+
228+
const data = null
229+
const result = stringify(data)
230+
231+
t.same(result, JSON.stringify(data))
232+
t.same(JSON.parse(result), data)
233+
})

0 commit comments

Comments
 (0)