Why Ajv? #675
-
It's a good validator, but the ergonomics leave a bit to be desired. Just to demonstrate, here's the default graph schema for Zodimport isSemVer from 'validator/lib/isSemVer';
import z from 'zod';
const PUBLIC = {
version: z.string().refine(isSemVer, {
message: 'Version must be a valid semantic version',
}),
main: z.string(),
typings: z.never({
invalid_type_error: '"typings" must only be set within the "publishConfig"',
}),
publishConfig: z
.object({
registry: z.string().optional(),
main: z.string().optional(),
module: z.string().optional(),
typings: z.string().optional(),
bin: z.union([z.string(), z.object({})]).optional(),
})
.optional(),
};
z.intersection(
z.object({ name: z.string() }),
z.discriminatedUnion('private', [
z.object({
private: z.literal(true),
version: z.never({
invalid_type_error: 'Private packages must not have version numbers',
}),
}),
z.object({ private: z.literal(false) }).extend(PUBLIC),
z.object({ private: z.undefined() }).extend(PUBLIC),
]),
) And here's the original: JSON Schema{
type: "object",
$required: true,
properties: {
name: { type: "string" },
main: { type: "string" },
},
required: ["name"],
if: {
properties: {
private: { type: "boolean", enum: [true] },
},
required: ["private"],
errorMessage: {},
},
then: {
properties: {
version: {
not: {},
errorMessage: {
_: "Private packages must not have version numbers.",
},
},
typings: { type: "string" },
},
},
else: {
properties: {
typings: {
not: {},
errorMessage: '"typings" must only be set withing the "publishConfig"',
},
publishConfig: {
type: "object",
properties: {
registry: { type: "string" },
main: { type: "string" },
module: { type: "string" },
typings: { type: "string" },
bin: { oneOf: [{ type: "string" }, { type: "object" }] },
},
},
version: {
type: "string",
// Official semver regex https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
pattern:
"^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$",
errorMessage: {
pattern: "Version must be a valid semantic version.",
},
},
},
required: ["version", "main"],
},
} The Zod version is actually not much more concise, and I was disappointed to find out that There are many alternatives. If performance is a concern, there's at least TypeBox. What are your thoughts? Edit: Using this for now: import type { PartialSchema } from 'ajv/dist/types/json-schema';
import type { GraphSchemaValidators } from 'onerepo';
import type { PackageJson } from 'type-fest';
export default {
'**': {
'package.json': {
// ...
} satisfies PartialSchema<PackageJson>,
},
} satisfies GraphSchemaValidators; Not perfect, as it has |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I love zod! AJV was chosen many years ago before zod was ever a thing and because it uses an actual standard. I'd be happy to re-evaluate this with some more discussion, but either:
|
Beta Was this translation helpful? Give feedback.
I love zod! AJV was chosen many years ago before zod was ever a thing and because it uses an actual standard.
I'd be happy to re-evaluate this with some more discussion, but either: