-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-ajv-validator.js
More file actions
executable file
·172 lines (148 loc) · 4.15 KB
/
generate-ajv-validator.js
File metadata and controls
executable file
·172 lines (148 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
const fs = require("node:fs/promises")
const path = require("node:path")
const AjvDraft04 = require("ajv-draft-04")
const Ajv2020 = require("ajv/dist/2020")
const standaloneCode = require("ajv/dist/standalone").default
const addFormats = require("ajv-formats")
const {Biome, Distribution} = require("@biomejs/js-api")
const json5 = require("json5")
const openapi30Path = path.join(
__dirname,
"../schemas/openapi-3.0-specification.json",
)
const openapi31Path = path.join(
__dirname,
"../schemas/openapi-3.1-specification.json",
)
const outputDir = path.join(
__dirname,
"../packages/openapi-code-generator/src/core/schemas",
)
const loadYamlFile = async (filepath) => {
const content = await fs.readFile(filepath, "utf-8")
return JSON.parse(content)
}
const writeOutput = async (filepath, moduleCode) => {
const raw = `
/** AUTOGENERATED - DO NOT EDIT **/
// @ts-nocheck
/* istanbul ignore file */
/* c8 ignore start */
${moduleCode}
/* c8 ignore end */
`
const biome = await Biome.create({
distribution: Distribution.NODE,
})
const {projectKey} = biome.openProject(
path.resolve(path.join(__dirname, "..")),
)
const biomeConfig = json5.parse(
await fs.readFile(path.join(__dirname, "../biome.jsonc"), "utf-8"),
)
biome.applyConfiguration(projectKey, biomeConfig)
const formatted = biome.formatContent(projectKey, raw, {
filePath: filepath,
})
await fs.writeFile(filepath, formatted.content, "utf-8")
}
const loadSchema = async (uri) => {
const res = await fetch(uri)
return res.json()
}
const compileOpenapi30Standalone = async () => {
const spec = await loadYamlFile(openapi30Path)
const ajv4 = new AjvDraft04({
code: {source: true},
strict: false,
loadSchema,
})
addFormats(ajv4)
const validate = ajv4.compile(spec)
return standaloneCode(ajv4, validate)
}
const compileOpenapi31Standalone = async (strict) => {
try {
const spec = await loadYamlFile(openapi31Path)
const ajv2020 = new Ajv2020({
code: {source: true},
strict: false,
verbose: true,
loadSchema,
})
addFormats(ajv2020)
ajv2020.addFormat("media-range", true)
const validate = ajv2020.compile(spec)
// TODO: it spits out a validator, but it doesn't actually work due to $dynamicAnchor not being supported
if (
!validate({
openapi: "3.1.0",
info: {
title: "Valid Specification",
version: "1.0.0",
},
paths: {
"/something": {
get: {
responses: {default: {description: "whatever"}},
},
},
},
components: {
schemas: {
Something: {
type: ["object", "null"],
properties: {
name: {type: "string"},
},
},
},
},
})
) {
const messages =
validate.errors?.map((err) => {
return [`-> ${err.message} at path '${err.instancePath}'`, err.params]
}) ?? []
if (strict) {
throw new Error(
`Validation failed: ${messages
.map((it) => `${it[0]} (${JSON.stringify(it[1])})`)
.join("\n")}`,
)
}
}
return standaloneCode(ajv2020, validate)
} catch (err) {
// TODO: MissingRefError: can't resolve reference https://spec.openapis.org/oas/3.1/schema/2022-10-07 from id https://spec.openapis.org/oas/3.1/schema-base/2022-10-07
console.warn(err.message)
console.warn(
"WARNING: failed to compile openapi 3.1 validator - using noop shim",
)
return `
"use strict"
const {logger} = require('../logger')
module.exports = validate
module.exports.default = validate
function validate(){
logger.warn(
"Skipping validation due to https://github.com/mnahkies/openapi-code-generator/issues/103",
)
return true
}
`
}
}
compileOpenapi30Standalone().then((output) =>
writeOutput(
path.join(outputDir, "openapi-3.0-specification-validator.ts"),
output,
),
)
compileOpenapi31Standalone(true).then((output) =>
writeOutput(
path.join(outputDir, "openapi-3.1-specification-validator.ts"),
output,
),
)