Skip to content

Commit

Permalink
test: rework base schemas
Browse files Browse the repository at this point in the history
test all examples with the latest base

Signed-off-by: Jonathan Alvarez <[email protected]>
  • Loading branch information
jonalvarezz committed Jun 5, 2024
1 parent d13d5cd commit 073ea68
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions packages/schemas/src/lib/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ function load(spec: VersionSpec): VersionData {
}
});

if (!FILENAME_TO_DEBUG) {
expect(baseSchema).not.toBeNull();
if (baseSchema === null) {
// swallow
// a base can be used in different schema versions.
}

return {
Expand All @@ -70,6 +71,7 @@ function load(spec: VersionSpec): VersionData {
const VERSION_DATA: VersionData[] = [
{ version: '1-0-0', compatibleVersionGlob: '1-0-0' },
{ version: '1-1-0', compatibleVersionGlob: '1-[01]-0' },
{ version: '1-1-1', compatibleVersionGlob: '1-[01]-[01]' },
].map(load);

class ValidationError extends Error {
Expand All @@ -93,37 +95,55 @@ afterAll(() => {
}
});

describe.each(VERSION_DATA)(
'Schema $version should accept $compatibleVersionGlob certificates',
({
version,
compatibleVersionGlob,
baseSchemaSpec,
credentialSchemaSpecs,
}) => {
test('base schema should accept all certificates', async () => {
describe('Base schemas', () => {
const baseVersionData: VersionSpec[] = [
{ version: '1-0-0', compatibleVersionGlob: '1-0-0' },
{ version: '1-1-0', compatibleVersionGlob: '*-*-*' },
];

const exceptions = [
{
comment: '20-token-holding-amount@1-1-0 allows empty assertions',
version: '1-1-0',
path: 'examples/1-1-0/20-token-holding-amount-list/20-token-holding-amount-list-empty.json',
},
];

const isException = (version: string, path: string) => {
return exceptions.some(
(exception) => exception.version === version && exception.path === path
);
};

test.each(baseVersionData)(
'base version $version should accept $compatibleVersionGlob credentials',
async ({ version, compatibleVersionGlob }) => {
// Import schema definition
const { schema } = await import(baseSchemaSpec.importPath);
const { schema } = await import(`../../lib/0-base/${version}`);
expect(schema.$id).toBeDefined();
expect(schema.$schema).toBeDefined();

// Compile schema
const validate = ajv.compile(schema);
expect(validate.errors).toBeNull();

const examplePaths = glob(
`../examples/${compatibleVersionGlob}/**/*.json`
);
const examplePaths = glob(`examples/${compatibleVersionGlob}/**/*.json`);

for (const examplePath of examplePaths) {
const example = JSON.parse(readFileSync(examplePath, 'utf8'));
const isValid = validate(example);

if (!isValid) {
fail(new ValidationError(examplePath, validate.errors));
if (!isValid && !isException(version, examplePath)) {
throw new ValidationError(examplePath, validate.errors);
}
}
});
}
);
});

describe.each(VERSION_DATA)(
'Schema $version should accept $compatibleVersionGlob certificates',
({ version, compatibleVersionGlob, credentialSchemaSpecs }) => {
test.each(credentialSchemaSpecs)(
'$name schema should accept $name certificates',
async ({ name, importPath }) => {
Expand Down

0 comments on commit 073ea68

Please sign in to comment.