tethys.backend/start/rules/translated_language.ts
Arno Kaimbacher ec17d79cf2
Some checks failed
CI Pipeline / japa-tests (push) Failing after 56s
- replaced validation library @adonisjs/validator with @vinejs/vine (performance)
- npm updates
2024-05-16 13:47:06 +02:00

49 lines
1.7 KiB
TypeScript

/*
|--------------------------------------------------------------------------
| 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));
});