tethys.backend/start/rules/unique.ts
Arno Kaimbacher 49bd96ee77
Some checks failed
CI Pipeline / japa-tests (push) Failing after 1m8s
feat: enhance user management, mimetype creation, and validation
- **AdminuserController.ts**: enable editing `first_name` and `last_name` for user creation and updates
- **MimetypeController.ts**: add creation support for mimetypes with selectable extensions
- **Models**: add `Mimetype` model (mime_type.ts); add `SnakeCaseNamingStrategy` for User model
- **Validators**:
  - **updateDatasetValidator**: increase title length to 255 and description length to 2500
  - **User Validators**: refine `createUserValidator` and `updateUserValidator` to include `first_name` and `last_name`
- **vanilla_error_reporter**: improve error reporting for wildcard fields
- **SKOS Query**: refine keyword request in `SearchCategoryAutocomplete.vue`
- **UI Enhancements**:
  - improve icon design in wizard (Wizard.vue)
  - add components for mimetype creation (Create.vue and button in Index.vue)
- **Routes**: update `routes.ts` to include new AdonisJS routes
2024-10-31 11:02:36 +01:00

61 lines
1.7 KiB
TypeScript

/*
|--------------------------------------------------------------------------
| Preloaded File - node ace make:preload rules/unique
|--------------------------------------------------------------------------
|*/
import { FieldContext } from '@vinejs/vine/types';
import db from '@adonisjs/lucid/services/db';
import vine from '@vinejs/vine';
import { VineString, VineNumber } from '@vinejs/vine';
/**
* Options accepted by the unique rule
*/
type Options = {
table: string;
column: string;
whereNot?: ((field: FieldContext) => string);
};
async function isUnique(value: unknown, options: Options, field: FieldContext) {
if (typeof value !== 'string' && typeof value != 'number') {
return;
}
let ignoreId: string | undefined;
if (options.whereNot) {
ignoreId = options.whereNot(field);
}
const builder = db.from(options.table).select(options.column).where(options.column, value);
if (ignoreId) {
builder.whereNot('id', '=', ignoreId);
}
const result = await builder.first();
if (result) {
// report that value is NOT unique
field.report('The {{ field }} field is not unique', 'isUnique', field);
// field.report(messages.unique, "isUnique", field);
}
}
export const isUniqueRule = vine.createRule(isUnique);
declare module '@vinejs/vine' {
interface VineString {
isUnique(options: Options): this;
}
interface VineNumber {
isUnique(options: Options): this;
}
}
VineString.macro('isUnique', function (this: VineString, options: Options) {
return this.use(isUniqueRule(options));
});
VineNumber.macro('isUnique', function (this: VineNumber, options: Options) {
return this.use(isUniqueRule(options));
});