Skip to content

Commit 8ba23ff

Browse files
authored
fix: #330 extra quotes on string test (#331)
* fix: #330 extra quotes on string test * fix: format * review
1 parent 2cc2b8a commit 8ba23ff

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ And nested ones, too.
139139
| `date` | `2020-04-03` |
140140
| `time` | `09:11:08` |
141141

142+
**Note**: In the case of string formatted Date and not Date Object, there will be no manipulation on it. It should be properly formatted.
143+
142144
Example with a MomentJS object:
143145

144146
```javascript

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function $asDatetime (date, skipQuotes) {
292292
} else if (date && typeof date.toISOString === 'function') {
293293
return quotes + date.toISOString() + quotes
294294
} else {
295-
return $asString(date)
295+
return $asString(date, skipQuotes)
296296
}
297297
}
298298

@@ -306,7 +306,7 @@ function $asDate (date, skipQuotes) {
306306
} else if (date && typeof date.format === 'function') {
307307
return quotes + date.format('YYYY-MM-DD') + quotes
308308
} else {
309-
return $asString(date)
309+
return $asString(date, skipQuotes)
310310
}
311311
}
312312

@@ -320,20 +320,26 @@ function $asTime (date, skipQuotes) {
320320
} else if (date && typeof date.format === 'function') {
321321
return quotes + date.format('HH:mm:ss') + quotes
322322
} else {
323-
return $asString(date)
323+
return $asString(date, skipQuotes)
324324
}
325325
}
326326

327-
function $asString (str) {
327+
function $asString (str, skipQuotes) {
328+
const quotes = skipQuotes === true ? '' : '"'
328329
if (str instanceof Date) {
329-
return '"' + str.toISOString() + '"'
330+
return quotes + str.toISOString() + quotes
330331
} else if (str === null) {
331-
return '""'
332+
return quotes + quotes
332333
} else if (str instanceof RegExp) {
333334
str = str.source
334335
} else if (typeof str !== 'string') {
335336
str = str.toString()
336337
}
338+
// If we skipQuotes it means that we are using it as test
339+
// no need to test the string length for the render
340+
if (skipQuotes) {
341+
return str
342+
}
337343

338344
if (str.length < 42) {
339345
return $asStringSmall(str)

test/anyof.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ test('anyOf with enum with more than 100 entries', (t) => {
448448
t.equal(value, '["EUR","USD",null]')
449449
})
450450

451-
test('anyOf object with field of type string with format or null', (t) => {
451+
test('anyOf object with field date-time of type string with format or null', (t) => {
452452
t.plan(1)
453453
const toStringify = new Date()
454454
const withOneOfSchema = {
@@ -471,3 +471,49 @@ test('anyOf object with field of type string with format or null', (t) => {
471471
prop: toStringify
472472
}), `{"prop":"${toStringify.toISOString()}"}`)
473473
})
474+
475+
test('anyOf object with field date of type string with format or null', (t) => {
476+
t.plan(1)
477+
const toStringify = '2011-01-01'
478+
const withOneOfSchema = {
479+
type: 'object',
480+
properties: {
481+
prop: {
482+
anyOf: [{
483+
type: 'string',
484+
format: 'date'
485+
}, {
486+
type: 'null'
487+
}]
488+
}
489+
}
490+
}
491+
492+
const withOneOfStringify = build(withOneOfSchema)
493+
t.equal(withOneOfStringify({
494+
prop: toStringify
495+
}), '{"prop":"2011-01-01"}')
496+
})
497+
498+
test('anyOf object with invalid field date of type string with format or null', (t) => {
499+
t.plan(1)
500+
const toStringify = 'foo bar'
501+
const withOneOfSchema = {
502+
type: 'object',
503+
properties: {
504+
prop: {
505+
anyOf: [{
506+
type: 'string',
507+
format: 'date'
508+
}, {
509+
type: 'null'
510+
}]
511+
}
512+
}
513+
}
514+
515+
const withOneOfStringify = build(withOneOfSchema)
516+
t.equal(withOneOfStringify({
517+
prop: toStringify
518+
}), '{"prop":null}')
519+
})

0 commit comments

Comments
 (0)