Skip to content

Commit

Permalink
fix: use ajv for schema ref resolving (#4049)
Browse files Browse the repository at this point in the history
* fix: use ajv for schema ref resolving

* Update package.json

* Update package.json

* Generate error-serializer.js with fast-json-stringify@5

Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ivan-tymoshenko and mcollina committed Jun 22, 2022
1 parent 81f28ce commit ac02014
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
41 changes: 18 additions & 23 deletions lib/error-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,44 +72,44 @@ class Serializer {
return bool === null ? 'null' : this.asBoolean(bool)
}

asDatetime (date, skipQuotes) {
const quotes = skipQuotes === true ? '' : '"'
asDatetime (date) {
const quotes = '"'
if (date instanceof Date) {
return quotes + date.toISOString() + quotes
}
return this.asString(date, skipQuotes)
return this.asString(date)
}

asDatetimeNullable (date, skipQuotes) {
return date === null ? 'null' : this.asDatetime(date, skipQuotes)
asDatetimeNullable (date) {
return date === null ? 'null' : this.asDatetime(date)
}

asDate (date, skipQuotes) {
const quotes = skipQuotes === true ? '' : '"'
asDate (date) {
const quotes = '"'
if (date instanceof Date) {
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + quotes
}
return this.asString(date, skipQuotes)
return this.asString(date)
}

asDateNullable (date, skipQuotes) {
return date === null ? 'null' : this.asDate(date, skipQuotes)
asDateNullable (date) {
return date === null ? 'null' : this.asDate(date)
}

asTime (date, skipQuotes) {
const quotes = skipQuotes === true ? '' : '"'
asTime (date) {
const quotes = '"'
if (date instanceof Date) {
return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + quotes
}
return this.asString(date, skipQuotes)
return this.asString(date)
}

asTimeNullable (date, skipQuotes) {
return date === null ? 'null' : this.asTime(date, skipQuotes)
asTimeNullable (date) {
return date === null ? 'null' : this.asTime(date)
}

asString (str, skipQuotes) {
const quotes = skipQuotes === true ? '' : '"'
asString (str) {
const quotes = '"'
if (str instanceof Date) {
return quotes + str.toISOString() + quotes
} else if (str === null) {
Expand All @@ -119,11 +119,6 @@ class Serializer {
} else if (typeof str !== 'string') {
str = str.toString()
}
// If we skipQuotes it means that we are using it as test
// no need to test the string length for the render
if (skipQuotes) {
return str
}

if (str.length < 42) {
return this.asStringSmall(str)
Expand Down Expand Up @@ -185,7 +180,7 @@ class Serializer {
}

function anonymous0 (input) {
// main
// #

var obj = (input && typeof input.toJSON === 'function')
? input.toJSON()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"eslint-plugin-n": "^15.2.0",
"eslint-plugin-promise": "^6.0.0",
"fast-json-body": "^1.1.0",
"fast-json-stringify": "^4.2.0",
"fast-json-stringify": "^5.0.0",
"fastify-plugin": "^3.0.1",
"fluent-json-schema": "^3.1.0",
"form-data": "^4.0.0",
Expand Down Expand Up @@ -179,7 +179,7 @@
"dependencies": {
"@fastify/ajv-compiler": "^3.1.0",
"@fastify/error": "^3.0.0",
"@fastify/fast-json-stringify-compiler": "^3.0.1",
"@fastify/fast-json-stringify-compiler": "^4.0.0",
"abstract-logging": "^2.0.1",
"avvio": "^8.1.3",
"find-my-way": "^6.3.0",
Expand Down
2 changes: 1 addition & 1 deletion test/schema-feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ test('Should throw of the schema does not exists in output', t => {

fastify.ready(err => {
t.equal(err.code, 'FST_ERR_SCH_SERIALIZATION_BUILD')
t.match(err.message, /^Failed building the serialization schema for GET: \/:id, due to error Cannot read propert.*/) // error from fast-json-strinfigy
t.match(err.message, /^Failed building the serialization schema for GET: \/:id, due to error Cannot find reference.*/) // error from fast-json-strinfigy
})
})

Expand Down

0 comments on commit ac02014

Please sign in to comment.