Skip to content

Commit

Permalink
⬆️ upgrade ajv, migrate json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcpip committed Feb 11, 2024
1 parent 8eb0852 commit 7d83970
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/lib/index.js"
}
]
}
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = Serializer => {
if (value && value.type === 'Buffer' &&
Array.isArray(value.data) &&
Object.keys(value).length === 2)
return new Buffer(value.data).toString(bufferEncoding)
return Buffer.from(value.data).toString(bufferEncoding)

return value
}, jsonSpaces)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune-json-api",
"description": "JSON API serializer for Fortune.",
"version": "2.3.0",
"version": "2.3.1",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -20,7 +20,8 @@
"uri-templates": "^0.2.0"
},
"devDependencies": {
"ajv": "^4.11.8",
"ajv": "^8",
"ajv-formats": "^2.1.1",
"chalk": "^3.0.0",
"eslint": "^6.8.0",
"eslint-config-boss": "^1.0.6",
Expand Down
14 changes: 8 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const deepEqual = require('deep-equal')
const qs = require('querystring')
const Ajv = require('ajv')
const Ajv = require('ajv').default
const addFormats = require('ajv-formats').default

const run = require('tapdance')

Expand All @@ -11,6 +12,7 @@ const jsonApi = require('../lib')
const jsonApiResponseSchema = require('./json-api-response-schema.json')

const ajv = new Ajv({allErrors: true, v5: true})
addFormats(ajv)
const validate = ajv.compile(jsonApiResponseSchema)

const mediaType = 'application/vnd.api+json'
Expand Down Expand Up @@ -80,7 +82,7 @@ run((assert, comment) => {
name: 'Rover',
type: 'Chihuahua',
birthday: new Date().toJSON(),
picture: new Buffer('This is a string.').toString('base64'),
picture: Buffer.from('This is a string.').toString('base64'),
'is-neutered': true,
nicknames: [ 'Doge', 'The Dog' ],
'some-date': '2015-01-04T00:00:00.000Z'
Expand All @@ -102,7 +104,7 @@ run((assert, comment) => {
assert(response.body.data.type === 'animals', 'type is correct')
assert(response.body.data.attributes['is-neutered'] === true,
'inflected key value is correct')
assert(new Buffer(response.body.data.attributes.picture, 'base64')
assert(Buffer.from(response.body.data.attributes.picture, 'base64')
.toString() === 'This is a string.', 'buffer is correct')
assert(Date.now() - new Date(response.body.data.attributes.birthday)
.getTime() < 60 * 1000, 'date is close enough')
Expand Down Expand Up @@ -655,7 +657,7 @@ run((assert, comment) => {
name: 'Rover',
type: 'Chihuahua',
birthday: new Date().toJSON(),
picture: new Buffer('This is a string.').toString('base64'),
picture: Buffer.from('This is a string.').toString('base64'),
'is-neutered': true,
nicknames: [ 'Doge', 'The Dog' ],
'some-date': '2015-01-04T00:00:00.000Z'
Expand All @@ -677,7 +679,7 @@ run((assert, comment) => {
assert(response.body.data.type === 'animal', 'type is correct')
assert(response.body.data.attributes['is-neutered'] === true,
'inflected key value is correct')
assert(new Buffer(response.body.data.attributes.picture, 'base64')
assert(Buffer.from(response.body.data.attributes.picture, 'base64')
.toString() === 'This is a string.', 'buffer is correct')
assert(Date.now() - new Date(response.body.data.attributes.birthday)
.getTime() < 60 * 1000, 'date is close enough')
Expand Down Expand Up @@ -778,7 +780,7 @@ run((assert, comment) => {
name: 'Rover',
type: 'Chihuahua',
birthday: new Date().toJSON(),
picture: new Buffer('This is a string.').toString('base64'),
picture: Buffer.from('This is a string.').toString('base64'),
'is-neutered': true,
nicknames: [ 'Doge', 'The Dog' ],
'some-date': '2015-01-04T00:00:00.000Z'
Expand Down
16 changes: 8 additions & 8 deletions test/json-api-response-schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-07/schema",
"title": "JSON API Schema",
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
"oneOf": [
Expand Down Expand Up @@ -158,7 +158,7 @@
"self": {
"description": "A `self` member, whose value is a URL for the relationship itself (a \"relationship URL\"). This URL allows the client to directly manipulate the relationship. For example, it would allow a client to remove an `author` from an `article` without deleting the people resource itself.",
"type": "string",
"format": "uri"
"format": "uri-reference"
},
"related": {
"$ref": "#/definitions/link"
Expand All @@ -172,7 +172,7 @@
{
"description": "A string containing the link's URL.",
"type": "string",
"format": "uri"
"format": "uri-reference"
},
{
"type": "object",
Expand All @@ -183,7 +183,7 @@
"href": {
"description": "A string containing the link's URL.",
"type": "string",
"format": "uri"
"format": "uri-reference"
},
"meta": {
"$ref": "#/definitions/meta"
Expand Down Expand Up @@ -287,28 +287,28 @@
"first": {
"description": "The first page of data",
"oneOf": [
{ "type": "string", "format": "uri" },
{ "type": "string", "format": "uri-reference" },
{ "type": "null" }
]
},
"last": {
"description": "The last page of data",
"oneOf": [
{ "type": "string", "format": "uri" },
{ "type": "string", "format": "uri-reference" },
{ "type": "null" }
]
},
"prev": {
"description": "The previous page of data",
"oneOf": [
{ "type": "string", "format": "uri" },
{ "type": "string", "format": "uri-reference" },
{ "type": "null" }
]
},
"next": {
"description": "The next page of data",
"oneOf": [
{ "type": "string", "format": "uri" },
{ "type": "string", "format": "uri-reference" },
{ "type": "null" }
]
}
Expand Down

0 comments on commit 7d83970

Please sign in to comment.