tethys.backend/start/rules/referenceValidation.ts
Arno Kaimbacher a3031169ca
All checks were successful
CI / container-job (push) Successful in 36s
feat: Add alternate mimetype support, enhance validation for alternate mimetypes, and improve script loading performance
- mime_type.ts: Added a new column `public alternate_mimetype: string;`
- MimetypeController.ts: Extended validation and storage logic to accommodate the new `alternate_mimetype` attribute
- adonisrc.ts: Integrated new validation rule to validate user-provided mimetypes
- vite.ts: Set `defer: true` for script attributes to improve loading performance
- update_1_to_mime_types.ts: Added migration for the new `alternate_mimetype` column in the database
- UI improvements: Updated components such as AsideMenuLayer.vue, FormCheckRadioGroup.vue, MimeTypeInput.vue, NavBar.vue (lime-green background), NavBarMenu.vue, SectionBannerStarOnGitea.vue, Admin/mimetype/Create.vue, Admin/mimetype/Delete.vue, Admin/mimetype/Index.vue
- allowed_extensions_mimetype.ts: Enhanced rule to also check for alternate mimetypes
- referenceValidation.ts: Improved validation to allow only ISBNs with a '-' delimiter
- package-lock.json: Updated npm dependencie
2025-02-13 15:49:09 +01:00

115 lines
5.2 KiB
TypeScript

import { FieldContext } from '@vinejs/vine/types';
import vine from '@vinejs/vine';
import { VineString } from '@vinejs/vine';
import { default as axios } from 'axios';
import { ReferenceIdentifierTypes } from '#contracts/enums';
type Options = {
typeField: string;
};
// Function to check if DOI exists using the DOI API
async function checkDoiExists(doi: string): Promise<boolean> {
try {
const response = await axios.get(`${doi}`);
return response.status === 200; // If status is 200, DOI is valid
} catch (error) {
return false; // If request fails, DOI does not exist
}
}
// Function to check if ISBN exists using the Open Library API
async function checkIsbnExists(isbn: string): Promise<boolean> {
try {
const response = await axios.get(`https://isbnsearch.org/isbn/${isbn}`);
return response.data && response.status == 200; // If title is returned, ISBN is valid
} catch (error) {
return false; // If request fails, ISBN does not exist
}
}
async function validateReference(value: unknown, options: Options, field: FieldContext) {
if (typeof value !== 'string') {
return;
}
const type = field.parent[options.typeField];
if (type === ReferenceIdentifierTypes.URL) {
if (!/^https?:\/\/[^\s$.?#].[^\s]*$/.test(value)) {
field.report('The {{ field }} must be a valid URL', 'validateReference', field);
} else {
try {
const exists = await checkDoiExists(value);
if (!exists) {
field.report('The {{ field }} must be an existing DOI', 'validateReference', field);
}
} catch (error) {
field.report('Error checking DOI existence: ' + error.message, 'validateReference', field);
}
}
}
// Check if the value does not match the DOI pattern
// The regex pattern ^10.\d{4,9}\/[-._;()/:a-zA-Z0-9]+$ is designed to match valid DOI formats.
// - ^10. ensures that the string starts with "10."
// - \d{4,9} matches a sequence of 4 to 9 digits.
// - \/ matches the literal forward slash character.
// - [-._;()/:a-zA-Z0-9]+ matches one or more characters that can include uppercase and lowercase letters, digits, and a set of special characters.
// The i flag at the end of the regex makes the matching case-insensitive, meaning it will match both uppercase and lowercase letters.
// If the value does not match this pattern, the code inside the if block will execute.
else if (type === ReferenceIdentifierTypes.DOI) {
// Extract the DOI from the URL if it starts with 'https://doi.org/'
const doiPattern = /^https:\/\/doi\.org\/(10.\d{4,9}\/[-._;()/:a-zA-Z0-9]+)$/i;
const match = value.match(doiPattern);
const doi = match ? match[1] : value;
// Check if the extracted DOI or the value itself matches the DOI patter
if (!/^10.\d{4,9}\/[-._;()/:a-zA-Z0-9]+$/i.test(doi)) {
field.report('The {{ field }} must be a valid DOI', 'validateReference', field);
} else {
try {
const exists = await checkDoiExists(value);
if (!exists) {
field.report('The {{ field }} must be an existing DOI', 'validateReference', field);
}
} catch (error) {
field.report('Error checking DOI existence: ' + error.message, 'validateReference', field);
}
}
} else if (type === ReferenceIdentifierTypes.ISBN) {
const isbnRegex = /^(?:\d{1,5}-\d{1,7}-\d{1,7}-[\dX]$|97[89]-\d{1,5}-\d{1,7}-\d{1,7}-\d)$/;
if (!isbnRegex.test(value)) {
field.report('The {{ field }} must be a valid ISBN', 'validateReference', field);
} else {
try {
const exists = await checkIsbnExists(value);
if (!exists) {
field.report('The {{ field }} must be an existing ISBN', 'validateReference', field);
}
} catch (error) {
field.report('Error checking ISBN existence: ' + error.message, 'validateReference', field);
}
}
} else if (type === ReferenceIdentifierTypes.Handle && !/^\d{2,}.\d{4,9}\/[-._;()/:a-zA-Z0-9]+$/.test(value)) {
/// Extract the Handle from the URL if it contains '/handle/'
field.report('The {{ field }} must be a valid Handle', 'validateReference', field);
} else if (
type === ReferenceIdentifierTypes.URN &&
!/^urn:[a-zA-Z0-9][a-zA-Z0-9-]{0,31}:[a-zA-Z0-9()+,\-.:=@;$_!*'%/?#]+$/.test(value)
) {
field.report('The {{ field }} must be a valid URN', 'validateReference', field);
} else if (type === ReferenceIdentifierTypes.ISSN && !/^\d{4}-\d{3}[\dxX]$/.test(value)) {
field.report('The {{ field }} must be a valid ISSN', 'validateReference', field);
}
}
export const validateReferenceRule = vine.createRule(validateReference);
declare module '@vinejs/vine' {
interface VineString {
validateReference(options: Options): this;
}
}
VineString.macro('validateReference', function (this: VineString, options: Options) {
return this.use(validateReferenceRule(options));
});