Skip to content

Commit 10bf649

Browse files
authored
feat!: v2 (#3)
1 parent 548ce7f commit 10bf649

File tree

10 files changed

+75
-88
lines changed

10 files changed

+75
-88
lines changed

.github/FUNDING.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: metcoder95

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 0 additions & 38 deletions
This file was deleted.

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
node: [14.x, 16.x, 17.x]
9+
node: [14.x, 16.x, 18.x]
1010
name: Node ${{ matrix.node }}
1111
steps:
1212
- uses: actions/checkout@v1
@@ -15,6 +15,4 @@ jobs:
1515
with:
1616
node-version: ${{ matrix.node }}
1717
- run: npm install
18-
- run: npm run lint:ci
1918
- run: npm run test:ci
20-
- run: npm run typescript

.github/workflows/pre-release.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Pre-Release
2+
on:
3+
push:
4+
tags:
5+
- 'v*.*.*-beta**'
6+
- 'v*.*.*-rc**'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
name: Pre-Release Creation
14+
steps:
15+
- uses: actions/checkout@v1
16+
- uses: ncipollo/release-action@v1
17+
with:
18+
prerelease: true
19+
bodyFile: "CHANGELOG.md"

.github/workflows/release.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- 'v[0-9]+.[0-9]+.[0-9]+'
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
name: Release Creation
13+
steps:
14+
- uses: actions/checkout@v1
15+
- uses: ncipollo/release-action@v1
16+
with:
17+
bodyFile: "CHANGELOG.md"
18+
token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ const Ajv = require('ajv');
2828
const app = fastify();
2929
const validator = new Ajv({});
3030

31-
app.register(splitValidator, { defaultValidator: validator });
31+
await app.register(splitValidator, { defaultValidator: validator });
3232
```
3333

34+
>**Note**:
35+
> It is important to advice that with the new fastify@v4, all the route registration now happens asynchronously.
36+
> This change translates in a way that if any plugin is meant to set logic into the `onRoute` hook for manipulating
37+
> routes after registration, it is necessary to await until the plugin is fully loaded before proceeding with the next parts
38+
> of your route definition. Otherwise, this can lead to non-deterministic behaviours when the plugin will not the expected
39+
> effect on your fastify application.
40+
3441
**On Route**
3542

3643
- `schemaValidators`: an object with the HTTP parts as keys and the validators to be used for that part as values

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ function plugin (fastifyInstance, opts = {}, done) {
7777
}
7878

7979
module.exports = fp(plugin, {
80-
fastify: '>=3.24.1',
80+
fastify: '>=4',
8181
name: 'fastify-split-validator'
8282
})

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
"author": "MetCoder95 <[email protected]>",
3131
"license": "MIT",
3232
"devDependencies": {
33-
"@types/node": "^14.17.6",
34-
"fastify": "^3.24.1",
35-
"husky": "^7.0.2",
33+
"@types/node": "^16.11.62",
34+
"fastify": "^4.6.0",
35+
"husky": "^7.0.4",
3636
"proxyquire": "^2.1.3",
3737
"snazzy": "^9.0.0",
3838
"standard": "^16.0.3",
@@ -41,8 +41,8 @@
4141
"typescript": "^4.4"
4242
},
4343
"dependencies": {
44-
"ajv": "^8.6.3",
45-
"fastify-plugin": "^3.0.0"
44+
"ajv": "^8.11.0",
45+
"fastify-plugin": "^4.2.1"
4646
},
4747
"tsd": {
4848
"directory": "test"

test/index.test.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test('Should allow custom AJV instance for body', async t => {
102102

103103
const body = res.json()
104104

105-
t.equal(body.message, 'body must be array')
105+
t.equal(body.message, 'body/msg must be array')
106106
t.equal(
107107
res.statusCode,
108108
400,
@@ -147,7 +147,7 @@ test('Should allow custom AJV instance for params', async t => {
147147

148148
const body = res.json()
149149

150-
t.equal(body.message, 'params must be integer')
150+
t.equal(body.message, 'params/msg must be integer')
151151
t.equal(
152152
res.statusCode,
153153
400,
@@ -198,7 +198,7 @@ test('Should allow custom AJV instance for headers', async t => {
198198
// TODO: set into documentation that it's possible the
199199
// error formatter doesn't work as expected.
200200
// Custom one should be provided
201-
t.equal(body.message, 'headers must be integer')
201+
t.equal(body.message, 'headers/x-type must be integer')
202202
t.equal(
203203
res.statusCode,
204204
400,
@@ -261,7 +261,7 @@ test('Should work with referenced schemas (querystring)', async t => {
261261

262262
const body = res.json()
263263

264-
t.equal(body.message, 'querystring must be array')
264+
t.equal(body.message, 'querystring/msg must be array')
265265
t.equal(
266266
res.statusCode,
267267
400,
@@ -313,7 +313,7 @@ test('Should work with referenced schemas (params)', async t => {
313313

314314
const body = res.json()
315315

316-
t.equal(body.message, 'params must be integer')
316+
t.equal(body.message, 'params/id must be integer')
317317
t.equal(res.statusCode, 400, 'Should not coearce the string into integer')
318318
} catch (err) {
319319
t.error(err)
@@ -364,7 +364,7 @@ test('Should work with referenced schemas (headers)', async t => {
364364

365365
const body = res.json()
366366

367-
t.equal(body.message, 'headers must be integer')
367+
t.equal(body.message, 'headers/x-id must be integer')
368368
t.equal(res.statusCode, 400, 'Should not coearce the string into integer')
369369
} catch (err) {
370370
t.error(err)
@@ -417,7 +417,7 @@ test('Should work with referenced schemas (body)', async t => {
417417

418418
const body = res.json()
419419

420-
t.equal(body.message, 'body must be string')
420+
t.equal(body.message, 'body/msg must be string')
421421
t.equal(res.statusCode, 400, 'Should not coearce the string into integer')
422422
} catch (err) {
423423
t.error(err)
@@ -485,7 +485,7 @@ test('Should work with parent and same instance schemas', async t => {
485485

486486
const body = res.json()
487487

488-
t.equal(body.message, 'body must be string')
488+
t.equal(body.message, 'body/msg must be string')
489489
t.equal(res.statusCode, 400, 'Should not coearce the string into integer')
490490
} catch (err) {
491491
t.error(err)
@@ -543,7 +543,7 @@ test('Should work with parent schemas', async t => {
543543

544544
const body = res.json()
545545

546-
t.equal(body.message, 'body must be string')
546+
t.equal(body.message, 'body/msg must be string')
547547
t.equal(res.statusCode, 400, 'Should not coearce the string into integer')
548548
} catch (err) {
549549
t.error(err)
@@ -623,9 +623,9 @@ test('Should work with parent nested schemas', async t => {
623623
})
624624
])
625625

626-
t.equal(res1.json().message, 'querystring must be array')
626+
t.equal(res1.json().message, 'querystring/msg must be array')
627627
t.equal(res1.statusCode, 400, 'Should not coearce the string into array')
628-
t.equal(res2.json().message, 'headers must be integer')
628+
t.equal(res2.json().message, 'headers/x-another must be integer')
629629
t.equal(res2.statusCode, 400, 'Should not coearce the string into integer')
630630
} catch (err) {
631631
t.error(err)
@@ -705,9 +705,9 @@ test('Should handle parsing to querystring (query)', async t => {
705705
})
706706
])
707707

708-
t.equal(res1.json().message, 'querystring must be array')
708+
t.equal(res1.json().message, 'querystring/msg must be array')
709709
t.equal(res1.statusCode, 400, 'Should not coearce the string into array')
710-
t.equal(res2.json().message, 'headers must be integer')
710+
t.equal(res2.json().message, 'headers/x-another must be integer')
711711
t.equal(res2.statusCode, 400, 'Should not coearce the string into integer')
712712
} catch (err) {
713713
t.error(err)
@@ -784,7 +784,7 @@ test('Should use default plugin validator as fallback', async t => {
784784
}
785785
})
786786

787-
t.equal(res.json().message, 'querystring must be array')
787+
t.equal(res.json().message, 'querystring/msg must be array')
788788
t.equal(res.statusCode, 400, 'Should not coearce the string into array')
789789
t.ok(compileCalled, 'Should have called the default Ajv instance')
790790
} catch (err) {
@@ -807,6 +807,7 @@ test('Should always cache schema to default plugin validator', async t => {
807807
}
808808

809809
headerAjv.compile = schema => {
810+
console.log('called')
810811
customCompileCalled = true
811812
return headerDefaultCompile(schema)
812813
}
@@ -829,13 +830,13 @@ test('Should always cache schema to default plugin validator', async t => {
829830
}
830831
})
831832

832-
server.register((instance, opts, done) => {
833+
server.register(async (instance, opts, done) => {
833834
instance.addSchema({
834835
$id: 'another',
835836
type: 'integer'
836837
})
837838

838-
instance.register(proxiedPlugin, {})
839+
await instance.register(proxiedPlugin, {})
839840

840841
instance.post(
841842
'/',
@@ -862,8 +863,6 @@ test('Should always cache schema to default plugin validator', async t => {
862863
reply.send({ noop: 'noop' })
863864
}
864865
)
865-
866-
done()
867866
})
868867

869868
try {
@@ -872,10 +871,13 @@ test('Should always cache schema to default plugin validator', async t => {
872871
url: '/',
873872
query: {
874873
msg: ['string']
874+
},
875+
headers: {
876+
'x-another': 1
875877
}
876878
})
877879

878-
t.equal(res.json().message, 'querystring must be array')
880+
t.equal(res.json().message, 'querystring/msg must be array')
879881
t.equal(res.statusCode, 400, 'Should not coearce the string into array')
880882
t.ok(compileCalled, 'Should have called the default Ajv instance')
881883
t.ok(customCompileCalled, 'Should have called the custom Ajv instance')
@@ -948,7 +950,7 @@ test('Should use default provided validator as fallback', async t => {
948950
}
949951
})
950952

951-
t.equal(res.json().message, 'querystring must be array')
953+
t.equal(res.json().message, 'querystring/msg must be array')
952954
t.equal(res.statusCode, 400, 'Should not coearce the string into array')
953955
t.ok(compileCalled, 'Should have called the default Ajv instance')
954956
} catch (err) {

0 commit comments

Comments
 (0)