- replaced validation library @adonisjs/validator with @vinejs/vine (performance)
Some checks failed
CI Pipeline / japa-tests (push) Failing after 56s
Some checks failed
CI Pipeline / japa-tests (push) Failing after 56s
- npm updates
This commit is contained in:
parent
08c2edca3b
commit
ec17d79cf2
32 changed files with 1677 additions and 1670 deletions
48
start/rules/translated_language.ts
Normal file
48
start/rules/translated_language.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Preloaded File - node ace make:preload rules/translatedLanguage
|
||||
|--------------------------------------------------------------------------
|
||||
|*/
|
||||
|
||||
import { FieldContext } from '@vinejs/vine/types';
|
||||
import vine from '@vinejs/vine';
|
||||
import { VineString } from '@vinejs/vine';
|
||||
|
||||
/**
|
||||
* Options accepted by the unique rule
|
||||
*/
|
||||
type Options = {
|
||||
mainLanguageField: string;
|
||||
typeField: string;
|
||||
};
|
||||
|
||||
async function translatedLanguage(value: unknown, options: Options, field: FieldContext) {
|
||||
if (typeof value !== 'string' && typeof value != 'number') {
|
||||
return;
|
||||
}
|
||||
|
||||
// const type = validator.helpers.getFieldValue(typeField, root, tip); //'type' = 'Translated'
|
||||
const typeValue = vine.helpers.getNestedValue(options.typeField, field); //'Main' or 'Translated'
|
||||
|
||||
// const mainLanguage = validator.helpers.getFieldValue(mainLanguageField, root, tip);
|
||||
const mainLanguage = field.data[options.mainLanguageField]; // 'en' or 'de'
|
||||
|
||||
if (typeValue === 'Translated') {
|
||||
if (value === mainLanguage) {
|
||||
// report thattranlated language field is same as main language field of dataset
|
||||
field.report('The tranlated {{ field }} hast the same language seted as dataset language', 'translatedLanguage', field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const translatedLanguageRule = vine.createRule(translatedLanguage);
|
||||
|
||||
declare module '@vinejs/vine' {
|
||||
interface VineString {
|
||||
translatedLanguage(options: Options): this;
|
||||
}
|
||||
}
|
||||
|
||||
VineString.macro('translatedLanguage', function (this: VineString, options: Options) {
|
||||
return this.use(translatedLanguageRule(options));
|
||||
});
|
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