mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-06 12:06:58 +00:00
* wip * bump misskey-dev/eslint-plugin * lint fixes (backend) * lint fixes (frontend) * lint fixes (frontend-embed) * rollback nsfwjs to 4.2.0 ref: infinitered/nsfwjs#904 * rollback openapi-typescript to v6 v7でOpenAPIのバリデーションが入るようになった関係でスコープ外での変更が避けられないため一時的に戻した * lint fixes (misskey-js) * temporarily disable errored lint rule (frontend-shared) * fix lint * temporarily ignore errored file for lint (frontend-shared) * rollback simplewebauthn/server to 12.0.0 v13 contains breaking changes that require some decision making * lint fixes (frontend-shared) * build misskey-js with types * fix(backend): migrate simplewebauthn/server to v12 * fix(misskey-js/autogen): ignore indent rules to generate consistent output * attempt to fix test changes due to capricorn86/happy-dom#1617 (XMLSerializer now produces valid XML) * attempt to fix test changes due to capricorn86/happy-dom#1617 (XMLSerializer now produces valid XML) * fix test * fix test * fix test * Apply suggestions from code review Co-authored-by: anatawa12 <anatawa12@icloud.com> * bump summaly to v5.2.0 * update tabler-icons to v3.30.0-based --------- Co-authored-by: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> Co-authored-by: anatawa12 <anatawa12@icloud.com>
110 lines
2.5 KiB
TypeScript
110 lines
2.5 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import type { CellValue, GridCell } from '@/components/grid/cell.js';
|
|
import type { GridColumn } from '@/components/grid/column.js';
|
|
import type { GridRow } from '@/components/grid/row.js';
|
|
import { i18n } from '@/i18n.js';
|
|
|
|
export type ValidatorParams = {
|
|
column: GridColumn;
|
|
row: GridRow;
|
|
value: CellValue;
|
|
allCells: GridCell[];
|
|
};
|
|
|
|
export type ValidatorResult = {
|
|
valid: boolean;
|
|
message?: string;
|
|
};
|
|
|
|
export type GridCellValidator = {
|
|
name?: string;
|
|
ignoreViolation?: boolean;
|
|
validate: (params: ValidatorParams) => ValidatorResult;
|
|
};
|
|
|
|
export type ValidateViolation = {
|
|
valid: boolean;
|
|
params: ValidatorParams;
|
|
violations: ValidateViolationItem[];
|
|
};
|
|
|
|
export type ValidateViolationItem = {
|
|
valid: boolean;
|
|
validator: GridCellValidator;
|
|
result: ValidatorResult;
|
|
};
|
|
|
|
export function cellValidation(allCells: GridCell[], cell: GridCell, newValue: CellValue): ValidateViolation {
|
|
const { column, row } = cell;
|
|
const validators = column.setting.validators ?? [];
|
|
|
|
const params: ValidatorParams = {
|
|
column,
|
|
row,
|
|
value: newValue,
|
|
allCells,
|
|
};
|
|
|
|
const violations: ValidateViolationItem[] = validators.map(validator => {
|
|
const result = validator.validate(params);
|
|
return {
|
|
valid: result.valid,
|
|
validator,
|
|
result,
|
|
};
|
|
});
|
|
|
|
return {
|
|
valid: violations.every(v => v.result.valid),
|
|
params,
|
|
violations,
|
|
};
|
|
}
|
|
|
|
class ValidatorPreset {
|
|
required(): GridCellValidator {
|
|
return {
|
|
name: 'required',
|
|
validate: ({ value }): ValidatorResult => {
|
|
return {
|
|
valid: value !== null && value !== undefined && value !== '',
|
|
message: i18n.ts._gridComponent._error.requiredValue,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
regex(pattern: RegExp): GridCellValidator {
|
|
return {
|
|
name: 'regex',
|
|
validate: ({ value }): ValidatorResult => {
|
|
return {
|
|
valid: (typeof value !== 'string') || pattern.test(value.toString() ?? ''),
|
|
message: i18n.tsx._gridComponent._error.patternNotMatch({ pattern: pattern.source }),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
unique(): GridCellValidator {
|
|
return {
|
|
name: 'unique',
|
|
validate: ({ column, row, value, allCells }): ValidatorResult => {
|
|
const bindTo = column.setting.bindTo;
|
|
const isUnique = allCells
|
|
.filter(it => it.column.setting.bindTo === bindTo && it.row.index !== row.index)
|
|
.every(cell => cell.value !== value);
|
|
return {
|
|
valid: isUnique,
|
|
message: i18n.ts._gridComponent._error.notUnique,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
export const validators = new ValidatorPreset();
|