Skip to content

Commit 747c870

Browse files
committed
chore: Updated dependencies, code and tools.
1 parent 3dc3829 commit 747c870

File tree

9 files changed

+55
-49
lines changed

9 files changed

+55
-49
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- name: Checkout
9-
uses: actions/checkout@v1
9+
uses: actions/checkout@v2
1010
- name: Use Node.js LTS
11-
uses: actions/setup-node@v1
11+
uses: actions/setup-node@v2
1212
with:
13-
node-version: 14.x
13+
node-version: 12.x
1414
- name: Restore cached dependencies
15-
uses: actions/cache@v1
15+
uses: actions/cache@v2
1616
with:
1717
path: node_modules
1818
key: node-modules-${{ hashFiles('package.json') }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Package Version](https://img.shields.io/npm/v/fastify-http-errors-enhanced.svg)](https://npm.im/fastify-http-errors-enhanced)
44
[![Dependency Status](https://img.shields.io/david/ShogunPanda/fastify-http-errors-enhanced)](https://david-dm.org/ShogunPanda/fastify-http-errors-enhanced)
55
[![Build](https://github.com/ShogunPanda/fastify-http-errors-enhanced/workflows/CI/badge.svg)](https://github.com/ShogunPanda/fastify-http-errors-enhanced/actions?query=workflow%3ACI)
6-
[![Code Coverage](https://img.shields.io/codecov/c/gh/ShogunPanda/fastify-http-errors-enhanced?token=d0ae1643f35c4c4f9714a357f796d05d)](https://codecov.io/gh/ShogunPanda/fastify-http-errors-enhanced)
6+
[![Code Coverage](https://img.shields.io/codecov/c/gh/ShogunPanda/fastify-http-errors-enhanced?token=ep3IRURLnT)](https://codecov.io/gh/ShogunPanda/fastify-http-errors-enhanced)
77

88
A error handling plugin for Fastify that uses enhanced HTTP errors.
99

lib/validation.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ function convertValidationErrors(section, data, validationErrors) {
8787
if (key.startsWith('.')) {
8888
key = key.substring(1);
8989
}
90+
// Remove useless quotes
91+
/* istanbul ignore next */
9092
if (key.startsWith('[') && key.endsWith(']')) {
9193
key = key.substring(1, key.length - 1);
9294
}
@@ -148,19 +150,17 @@ function convertValidationErrors(section, data, validationErrors) {
148150
if (!message.length) {
149151
message = `${e.message.replace(/^should/, 'must')} (${e.keyword})`;
150152
}
151-
// Find the property to add
152-
let property = key
153-
.replace(/\[(\d+)\]/g, '.$1') // Array path
154-
.replace(/\[([^\]]+)\]/g, '.$1'); // Object path
155153
// Remove useless quotes
156-
if (property.match(/(?:^['"])(?:[^.]+)(?:['"]$)/)) {
157-
property = property.substring(1, property.length - 1);
154+
/* istanbul ignore next */
155+
if (key.match(/(?:^['"])(?:[^.]+)(?:['"]$)/)) {
156+
key = key.substring(1, key.length - 1);
158157
}
159158
// Fix empty properties
160-
if (!property) {
161-
property = '$root';
159+
if (!key) {
160+
key = '$root';
162161
}
163-
errors[property] = message;
162+
key = key.replace(/^\//, '');
163+
errors[key] = message;
164164
}
165165
return { [section]: errors };
166166
}
@@ -213,8 +213,7 @@ function compileResponseValidationSchema() {
213213
removeAdditional: false,
214214
useDefaults: true,
215215
coerceTypes: false,
216-
allErrors: true,
217-
nullable: true
216+
allErrors: true
218217
});
219218
compiler.addSchema(Object.values(instance.getSchemas()));
220219
for (const [code, schema] of schemas) {

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
"postpublish": "git push origin && git push origin -f --tags"
3838
},
3939
"dependencies": {
40-
"ajv": "^6.12.6",
40+
"ajv": "^7.0.2",
4141
"fastify-plugin": "^3.0.0",
42-
"http-errors-enhanced": "^0.5.0"
42+
"http-errors-enhanced": "^0.5.2"
4343
},
4444
"devDependencies": {
4545
"@cowtech/eslint-config": "^7.10.1",
46-
"@types/node": "^14.14.10",
46+
"@types/node": "^14.14.19",
4747
"@types/tap": "^14.10.1",
48-
"fastify": "^3.8.0",
48+
"ajv-formats": "^1.5.1",
49+
"fastify": "^3.9.2",
4950
"prettier": "^2.2.1",
5051
"tap": "^14.11.0",
51-
"typescript": "^4.1.2"
52+
"typescript": "^4.1.3"
5253
},
5354
"engines": {
5455
"node": ">=12.15.0"

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Ajv, ValidateFunction } from 'ajv'
1+
import Ajv, { ValidateFunction } from 'ajv'
22

33
export const kHttpErrorsEnhancedProperties = Symbol('fastify-http-errors-enhanced-properties')
44
export const kHttpErrorsEnhancedResponseValidations = Symbol('fastify-http-errors-enhanced-response-validation')

src/validation.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ export function convertValidationErrors(
106106
key = key.substring(1)
107107
}
108108

109+
// Remove useless quotes
110+
/* istanbul ignore next */
109111
if (key.startsWith('[') && key.endsWith(']')) {
110112
key = key.substring(1, key.length - 1)
111113
}
@@ -175,22 +177,20 @@ export function convertValidationErrors(
175177
message = `${e.message.replace(/^should/, 'must')} (${e.keyword})`
176178
}
177179

178-
// Find the property to add
179-
let property = key
180-
.replace(/\[(\d+)\]/g, '.$1') // Array path
181-
.replace(/\[([^\]]+)\]/g, '.$1') // Object path
182-
183180
// Remove useless quotes
184-
if (property.match(/(?:^['"])(?:[^.]+)(?:['"]$)/)) {
185-
property = property.substring(1, property.length - 1)
181+
/* istanbul ignore next */
182+
if (key.match(/(?:^['"])(?:[^.]+)(?:['"]$)/)) {
183+
key = key.substring(1, key.length - 1)
186184
}
187185

188186
// Fix empty properties
189-
if (!property) {
190-
property = '$root'
187+
if (!key) {
188+
key = '$root'
191189
}
192190

193-
errors[property] = message
191+
key = key.replace(/^\//, '')
192+
193+
errors[key] = message
194194
}
195195

196196
return { [section]: errors }
@@ -258,8 +258,7 @@ export function compileResponseValidationSchema(this: FastifyInstance): void {
258258
removeAdditional: false,
259259
useDefaults: true,
260260
coerceTypes: false,
261-
allErrors: true,
262-
nullable: true
261+
allErrors: true
263262
})
264263

265264
compiler.addSchema(Object.values(instance.getSchemas()))

test/index.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ async function buildServer(options: FastifyPluginOptions = {}): Promise<FastifyI
150150
removeAdditional: false,
151151
useDefaults: true,
152152
coerceTypes: true,
153-
allErrors: true,
154-
nullable: true
153+
allErrors: true
155154
}
156155
}
157156
})

test/validation.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

33
import Ajv from 'ajv'
4+
import addFormats from 'ajv-formats'
45
import fastify, { FastifyInstance, FastifyPluginOptions, FastifyReply, FastifyRequest, ValidationResult } from 'fastify'
56
import { ACCEPTED, INTERNAL_SERVER_ERROR, OK } from 'http-errors-enhanced'
67
import t from 'tap'
@@ -146,20 +147,27 @@ t.test('Validation', (t: Test) => {
146147
useDefaults: true,
147148
coerceTypes: true,
148149
allErrors: true,
149-
nullable: true,
150150
formats: {
151-
invalidResponseCode(raw: number): boolean {
152-
return raw < 100 && raw > 599
151+
invalidResponseCode: {
152+
validate(raw: number): boolean {
153+
return raw < 100 && raw > 599
154+
}
153155
},
154-
invalidResponse(raw: number): boolean {
155-
return raw < 100 && raw > 599
156+
invalidResponse: {
157+
validate(raw: number): boolean {
158+
return raw < 100 && raw > 599
159+
}
156160
},
157-
noMessage(): boolean {
158-
return false
161+
noMessage: {
162+
validate(): boolean {
163+
return false
164+
}
159165
}
160166
}
161167
})
162168

169+
addFormats(ajv)
170+
163171
const schema = {
164172
type: 'object',
165173
properties: {
@@ -225,7 +233,7 @@ t.test('Validation', (t: Test) => {
225233
},
226234
pattern: {
227235
type: 'string',
228-
pattern: '\\d+{a}abc'
236+
pattern: '\\d+\\{a\\}abc'
229237
},
230238
uuid: {
231239
type: 'string',
@@ -337,17 +345,17 @@ t.test('Validation', (t: Test) => {
337345
emptyArray: 'must be a empty array',
338346
minItems: 'must be an array with at least 2 items',
339347
maxItems: 'must be an array with at most 2 items',
340-
'arrayPath.0': 'must be a valid number',
341-
"objectPath.'x-abc'": 'must be a valid number',
342-
'objectPath.cde': 'must be a valid number',
348+
'arrayPath/0': 'must be a valid number',
349+
'objectPath/x-abc': 'must be a valid number',
350+
'objectPath/cde': 'must be a valid number',
343351
minimum: 'must be a number greater than or equal to 5',
344352
maximum: 'must be a number less than or equal to 5',
345353
integer: 'must be a valid integer number',
346354
object: 'must be a object',
347355
number: 'must be a valid number',
348356
enum: 'must be one of the following values: "a", "b" or "c"',
349357
presentString: 'must be a non empty string',
350-
pattern: 'must match pattern "\\d+{a}abc"',
358+
pattern: 'must match pattern "\\d+\\{a\\}abc"',
351359
uuid: 'must be a valid GUID (UUID v4)',
352360
hostname: 'must be a valid hostname',
353361
ipv4: 'must be a valid IPv4',

types/interfaces.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="node" />
2-
import { Ajv, ValidateFunction } from 'ajv';
2+
import Ajv, { ValidateFunction } from 'ajv';
33
export declare const kHttpErrorsEnhancedProperties: unique symbol;
44
export declare const kHttpErrorsEnhancedResponseValidations: unique symbol;
55
declare module 'fastify' {

0 commit comments

Comments
 (0)