All checks were successful
CI / container-job (push) Successful in 43s
- ace.js: use ts-node-maintained - adonisrc.ts: load vite_provider, sett assetBundler to false, addd hooks property - Dockerfile: change to node version 22 - package.json: remove babel depencies; add @swc/wasm, add vitejs/plugin-vue, add hot-hook, add vite, update eslint-config-prettier, tailwindcss, ts-node-maintained - new vite.config.js and config/vite.ts - inertia.js - improved own vinejs_provider.ts - adapted app.css needed for vitejs - adapted app.ts: new resolve method neede for vitejs relocated resources/js/logo.svg - remove Buffer import into FileUpload.vue - Create.vue: improved submit needed for @inertiajs/vue3 form helper - Edit.vue: mproved submit needed for @inertiajs/vue3 form helper - kernel.ts: load vite_middleware - formated rotes.ts file - rewritten allowed_extensions_mimetypes.ts file (removed typescript errors)
61 lines
1.9 KiB
TypeScript
61 lines
1.9 KiB
TypeScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Preloaded File - node ace make:preload rules/uniquePerson
|
|
|--------------------------------------------------------------------------
|
|
|*/
|
|
|
|
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);
|
|
idField: string;
|
|
};
|
|
|
|
async function isUniquePerson(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 idValue = vine.helpers.getNestedValue(options.idField, field); //'Main' or 'Translated'
|
|
|
|
const builder = db.from(options.table).select(options.column).where(options.column, value);
|
|
// if existent id, exclide it from validating
|
|
if (idValue) {
|
|
builder.whereNot('id', '=', idValue);
|
|
}
|
|
const result = await builder.first();
|
|
if (result) {
|
|
// report that value is NOT unique
|
|
field.report('The {{ field }} field is not unique', 'isUnique', field);
|
|
}
|
|
}
|
|
|
|
export const isUniquePersonRule = vine.createRule(isUniquePerson);
|
|
|
|
declare module '@vinejs/vine' {
|
|
interface VineString {
|
|
isUniquePerson(options: Options): this;
|
|
}
|
|
interface VineNumber {
|
|
isUniquePerson(options: Options): this;
|
|
}
|
|
}
|
|
|
|
VineString.macro('isUniquePerson', function (this: VineString, options: Options) {
|
|
return this.use(isUniquePersonRule(options));
|
|
});
|
|
VineNumber.macro('isUniquePerson', function (this: VineNumber, options: Options) {
|
|
return this.use(isUniquePersonRule(options));
|
|
});
|