feat: Enhance reference validation and add support for Handle URLs
Some checks failed
CI Pipeline / japa-tests (push) Failing after 51s

- Updated reference validation to handle various identifier types including DOI, ISBN, ISSN, URN, and Handle.
- Improved regex patterns for DOI and Handle validation to correctly extract and validate identifiers from URLs.
- Added asynchronous checks to verify the existence of DOI and Handle URLs.
- Added asynchronous checks to verify the existence of  ISBNs
- Included detailed comments explaining the regex patterns and validation logic.
- Adjusted the validation logic to handle any URL prefix for Handle identifiers.
- Ensured that the Handle format `handle/20.500.12854/36478` is correctly validated.
- Updated the CI workflow to trigger on push and pull request events.
This commit is contained in:
Kaimbacher 2025-01-24 17:11:10 +01:00
parent 537c6fd81a
commit 2c4f51be68
12 changed files with 538 additions and 162 deletions

View file

@ -15,7 +15,7 @@ import { assert } from '@japa/assert';
import { apiClient } from '@japa/api-client';
import { pluginAdonisJS } from '@japa/plugin-adonisjs';
import app from '@adonisjs/core/services/app';
import env from '#start/env'
/*
|--------------------------------------------------------------------------
| Japa Plugins
@ -28,7 +28,13 @@ import app from '@adonisjs/core/services/app';
|
*/
// export const plugins: Required<Config>['plugins'] = [assert(), runFailedTests(), apiClient()];
export const plugins: Config['plugins'] = [assert(), apiClient(), pluginAdonisJS(app)];
export const plugins: Config['plugins'] = [
assert(),
apiClient({
baseURL: `http://${env.get('HOST')}:${env.get('PORT')}`,
}),
pluginAdonisJS(app),
];
/*
|--------------------------------------------------------------------------
@ -59,11 +65,18 @@ export const reporters: Required<Config>['reporters'] = {
*/
export const runnerHooks: Pick<Required<Config>, 'setup' | 'teardown'> = {
setup: [
() => {
console.log('running before all the tests');
},
// () => testUtils.ace().loadCommands(),
() => testUtils.db().migrate(),
// () => testUtils.httpServer().start(),
],
teardown: [],
teardown: [
() => {
console.log('running after all the tests');
},
],
};
/*

View file

@ -0,0 +1,215 @@
import { test } from '@japa/runner';
import { ReferenceIdentifierTypes } from '#contracts/enums';
import vine from '@vinejs/vine';
// node ace test functional --groups "ReferenceValidation"
test.group('ReferenceValidation', () => {
test('validate valid DOI', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
}),
);
const data = {
reference: 'https://doi.org/10.24341/tethys.236',
type: ReferenceIdentifierTypes.DOI,
};
try {
const payload = await validator.validate(data);
assert.deepEqual(payload, data);
} catch {
assert.isTrue(false);
}
});
test('validate invalid DOI', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
}),
);
const data = {
reference: 'https://doi.org/invalid-doi',
type: ReferenceIdentifierTypes.DOI,
};
let payload = {};
try {
payload = await validator.validate(data);
} catch {
assert.notDeepEqual(payload, data);
}
});
test('validate valid Handle', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: '20.5000/abc123',
type: ReferenceIdentifierTypes.Handle,
};
try {
const payload = await validator.validate(data);
assert.deepEqual(payload, data);
} catch {
assert.isTrue(false);
}
});
test('validate valid ISBN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
// reference: '978-3-85316-090-9',
// reference: '9783853160909',
// reference: '978-3-900312-64-0', // Geologische Karte der Republik Österreich 1 : 50.000
reference: '3900312648', // Geologische Karte der Republik Österreich 1 : 50.000
type: ReferenceIdentifierTypes.ISBN,
};
try {
const payload = await validator.validate(data);
assert.deepEqual(payload, data);
} catch {
assert.isTrue(false);
}
});
test('validate invalid ISBN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: 'invalid-isbn',
type: ReferenceIdentifierTypes.ISBN,
};
let payload = {};
try {
payload = await validator.validate(data);
} catch {
assert.notDeepEqual(payload, data);
}
});
test('validate valid URN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: 'urn:isbn:0451450523',
type: ReferenceIdentifierTypes.URN,
};
try {
const payload = await validator.validate(data);
assert.deepEqual(payload, data);
} catch {
assert.isTrue(false);
}
});
test('validate invalid URN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: 'invalid-urn',
type: ReferenceIdentifierTypes.URN,
};
let payload = {};
try {
payload = await validator.validate(data);
} catch {
assert.notDeepEqual(payload, data);
}
});
test('validate valid ISSN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: '1234-567X',
type: ReferenceIdentifierTypes.ISSN,
};
try {
const payload = await validator.validate(data);
assert.deepEqual(payload, data);
} catch {
assert.isTrue(false);
}
});
test('validate invalid ISSN', async ({ assert }) => {
const validator = vine.compile(
vine.object({
reference: vine.string().validateReference({ typeField: 'type' }),
type: vine.enum(Object.values(ReferenceIdentifierTypes)),
})
);
const data = {
reference: 'invalid-issn',
type: ReferenceIdentifierTypes.ISSN,
};
let payload = {};
try {
payload = await validator.validate(data);
} catch {
assert.notDeepEqual(payload, data);
}
});
// test('validate invalid Handle', async ({ assert }) => {
// const validator = vine.compile(
// vine.object({
// reference: vine.string().validateReference({ typeField: 'type' }),
// type: vine.enum(Object.values(ReferenceIdentifierTypes)),
// })
// );
// const data = {
// reference: 'invalid-handle',
// type: ReferenceIdentifierTypes.Handle,
// };
// const result = await validator.validate(data);
// assert.isFalse(result.valid);
// });
// Add more tests for other reference types as needed
});