forked from geolba/tethys.backend
- replaced validation library @adonisjs/validator with @vinejs/vine (performance)
- npm updates
This commit is contained in:
parent
08c2edca3b
commit
ec17d79cf2
32 changed files with 1677 additions and 1670 deletions
60
start/rules/unique.ts
Normal file
60
start/rules/unique.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue