Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: data validation using schema #30797

Merged
1 change: 1 addition & 0 deletions lib/data/monorepo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tools/schemas/monorepo-schema.json",
"repoGroups": {
"accounts": "https://github.com/accounts-js/accounts",
"acot": "https://github.com/acot-a11y/acot",
Expand Down
51 changes: 51 additions & 0 deletions test/validate-schemas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fs from 'fs-extra';
import upath from 'upath';
import { Json } from '../lib/util/schema-utils';
import { capitalize } from '../tools/docs/utils';
import * as Schemas from '../tools/schemas/schema';

const dataFileDir = 'lib/data';
const schemaDir = 'tools/schemas';
const schemasAndJsonFiles: {
schemaName: keyof typeof Schemas;
dataFileName: string;
}[] = [];
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved

describe('validate-schemas', () => {
it('validate json files in lib/data against their schemas', async () => {
const schemaFiles = (await fs.readdir(schemaDir)).filter(
(file) => upath.extname(file) === '.json',
);

for (const schemaFile of schemaFiles) {
const correspondingDatFileName = schemaFile.replace('-schema', '');
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const schemaName = `${schemaFile
.replace('.json', '')
.split('-')
.map(capitalize)
.join('')}` as keyof typeof Schemas;
schemasAndJsonFiles.push({
schemaName,
dataFileName: correspondingDatFileName,
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});
}

await Promise.all(
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
schemasAndJsonFiles.map(async ({ schemaName, dataFileName }) => {
const data = Json.parse(
await fs.readFile(upath.join(dataFileDir, dataFileName), 'utf8'),
);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved

let result: Record<string, unknown> =
// eslint-disable-next-line import/namespace
Schemas[schemaName].safeParse(data);
result = { ...result, dataFileName, schemaName };
expect(result).toMatchObject({
dataFileName,
schemaName,
success: true,
});
}),
);
});
});
53 changes: 53 additions & 0 deletions tools/schemas/monorepo-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"repoGroups": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9. -]+$": {
"oneOf": [
{ "type": "string", "format": "uri" },
{
"type": "array",
"items": { "type": "string", "format": "uri" }
}
]
}
},
"additionalProperties": false
},
"orgGroups": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9. -]+$": {
"oneOf": [
{ "type": "string", "format": "uri" },
{
"type": "array",
"items": { "type": "string", "format": "uri" }
}
]
}
},
"additionalProperties": false
},
"patternGroups": {
"type": "object",
"patternProperties": {
"^[a-zA-Z0-9. -]+$": {
"oneOf": [
{ "type": "string", "pattern": "^/.+/$" },
{
"type": "array",
"items": { "type": "string", "pattern": "^/.+/$" }
}
]
}
},
"additionalProperties": false
}
},
"required": ["repoGroups", "orgGroups", "patternGroups"],
"additionalProperties": false
}
14 changes: 14 additions & 0 deletions tools/schemas/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { z } from 'zod';

const UrlSchema = z.record(
z.string(),
z.union([z.string(), z.array(z.string())]),
);

const MonorepoSchema = z.object({
repoGroups: UrlSchema,
orgGroups: UrlSchema,
patternGroups: UrlSchema,
});

export { MonorepoSchema };