Skip to content

Commit 7cd4d3c

Browse files
committed
feat: move EF validation logic to edge-bundler
1 parent 68feefc commit 7cd4d3c

File tree

12 files changed

+52
-287
lines changed

12 files changed

+52
-287
lines changed

package-lock.json

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/build/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
"@netlify/run-utils": "^5.0.2",
7474
"@netlify/zip-it-and-ship-it": "^7.1.2",
7575
"@sindresorhus/slugify": "^2.0.0",
76-
"ajv": "^8.11.0",
77-
"ajv-errors": "^3.0.0",
7876
"ansi-escapes": "^5.0.0",
7977
"chalk": "^5.0.0",
8078
"clean-stack": "^4.0.0",

packages/build/src/plugins_core/edge_functions/validate_manifest/validate_edge_functions_manifest.js

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { promises as fs } from 'fs'
2+
import { join, resolve } from 'path'
3+
4+
import { ManifestValidationError, validateManifest } from '@netlify/edge-bundler'
5+
6+
import { addErrorInfo } from '../../../error/info.js'
7+
8+
export const validateEdgeFunctionsManifest = async function ({
9+
buildDir,
10+
constants: { EDGE_FUNCTIONS_DIST: distDirectory },
11+
}: {
12+
buildDir: string
13+
constants: { EDGE_FUNCTIONS_DIST: string }
14+
}) {
15+
const edgeFunctionsDistPath = resolve(buildDir, distDirectory)
16+
const manifestPath = join(edgeFunctionsDistPath, 'manifest.json')
17+
const data = await fs.readFile(manifestPath)
18+
// @ts-expect-error TypeScript is not aware that parse can handle Buffer
19+
const manifestData = JSON.parse(data)
20+
21+
try {
22+
validateManifest(manifestData)
23+
} catch (error) {
24+
if (error instanceof ManifestValidationError) {
25+
addErrorInfo(error, { type: 'coreStep' })
26+
}
27+
28+
throw error
29+
}
30+
31+
return {}
32+
}

packages/build/tests/unit/validate_edge_manifest/snapshots/tests.js.md

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,22 @@ The actual snapshot is saved in `tests.js.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

7-
## should detect invalid route pattern in manifest
7+
## should print error on invalid manifest
88

99
> Snapshot 1
1010
11-
`[␊
12-
{␊
13-
"instancePath": "/routes/2/pattern",␊
14-
"schemaPath": "#/properties/routes/items/properties/pattern/errorMessage",␊
15-
"keyword": "errorMessage",␊
16-
"params": {␊
17-
"errors": [␊
18-
{␊
19-
"instancePath": "/routes/2/pattern",␊
20-
"schemaPath": "#/properties/routes/items/properties/pattern/format",␊
21-
"keyword": "format",␊
22-
"params": {␊
23-
"format": "regexPattern"␊
24-
},␊
25-
"message": "must match format \\"regexPattern\\"",␊
26-
"emUsed": true␊
27-
}␊
28-
]␊
29-
},␊
30-
"message": "must match format /^\\\\^.*\\\\$$/"␊
31-
}␊
32-
]`
11+
`Validation of Edge Functions manifest failed␊
12+
TYPE must be object␊
13+
14+
> 1 | "json"␊
15+
  | ^^^^^^ 👈🏽 type must be object`
3316

34-
## should detect missing property in manifest
17+
## should print error on empty manifest
3518

3619
> Snapshot 1
3720
38-
`[␊
39-
{␊
40-
"instancePath": "",␊
41-
"schemaPath": "#/errorMessage",␊
42-
"keyword": "errorMessage",␊
43-
"params": {␊
44-
"errors": [␊
45-
{␊
46-
"instancePath": "/bundles/0",␊
47-
"schemaPath": "#/properties/bundles/items/required",␊
48-
"keyword": "required",␊
49-
"params": {␊
50-
"missingProperty": "format"␊
51-
},␊
52-
"message": "must have required property 'format'",␊
53-
"emUsed": true␊
54-
}␊
55-
]␊
56-
},␊
57-
"message": "Couldn't validate Edge Functions manifest.json"␊
58-
}␊
59-
]`
60-
61-
## should detect extra property in manifest
62-
63-
> Snapshot 1
64-
65-
`[␊
66-
{␊
67-
"instancePath": "",␊
68-
"schemaPath": "#/errorMessage",␊
69-
"keyword": "errorMessage",␊
70-
"params": {␊
71-
"errors": [␊
72-
{␊
73-
"instancePath": "/routes/0",␊
74-
"schemaPath": "#/properties/routes/items/additionalProperties",␊
75-
"keyword": "additionalProperties",␊
76-
"params": {␊
77-
"additionalProperty": "extra"␊
78-
},␊
79-
"message": "must NOT have additional properties",␊
80-
"emUsed": true␊
81-
}␊
82-
]␊
83-
},␊
84-
"message": "Couldn't validate Edge Functions manifest.json"␊
85-
}␊
86-
]`
21+
`Validation of Edge Functions manifest failed␊
22+
REQUIRED must have required property 'bundler_version'␊
23+
24+
> 1 | {}␊
25+
  | ^ ☹️ bundler_version is missing here!`
Binary file not shown.

packages/build/tests/unit/validate_edge_manifest/tests.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,22 @@ test('should validate valid manifest', async (t) => {
1515
)
1616
})
1717

18-
test('should detect invalid route pattern in manifest', async (t) => {
18+
test('should print error on invalid manifest', async (t) => {
1919
const error = await t.throwsAsync(
2020
validateEdgeFunctionsManifest({
2121
buildDir: FIXTURES_DIR,
22-
constants: { EDGE_FUNCTIONS_DIST: 'invalid_manifest_wrong_route_pattern' },
22+
constants: { EDGE_FUNCTIONS_DIST: 'invalid_manifest' },
2323
}),
2424
)
2525

2626
t.snapshot(error.message)
2727
})
2828

29-
test('should detect missing property in manifest', async (t) => {
29+
test('should print error on empty manifest', async (t) => {
3030
const error = await t.throwsAsync(
3131
validateEdgeFunctionsManifest({
3232
buildDir: FIXTURES_DIR,
33-
constants: { EDGE_FUNCTIONS_DIST: 'invalid_manifest_missing_property' },
34-
}),
35-
)
36-
37-
t.snapshot(error.message)
38-
})
39-
40-
test('should detect extra property in manifest', async (t) => {
41-
const error = await t.throwsAsync(
42-
validateEdgeFunctionsManifest({
43-
buildDir: FIXTURES_DIR,
44-
constants: { EDGE_FUNCTIONS_DIST: 'invalid_manifest_extra_property' },
33+
constants: { EDGE_FUNCTIONS_DIST: 'empty_manifest' },
4534
}),
4635
)
4736

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bundler_version": 123
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"json"

packages/build/tests/unit/validate_edge_manifest/unit_fixtures/invalid_manifest_extra_property/manifest.json

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

0 commit comments

Comments
 (0)