import type { VmRecord, VmValue } from '@mirascript/mirascript'; import type { ArgumentValue, ConditionExpression, ConditionFunction, ArgumentFunction } from './argument.js'; import type { ExpressionSource } from '../expression.js'; export const ConstraintLevel = Object.freeze(['info', 'warning', 'error'] as const); /** 约束条件的级别 */ export type ConstraintLevel = (typeof ConstraintLevel)[number]; /** 表示参数值需要满足的约束条件 */ export interface Constraint { /** * 指定约束条件检查顺序,顺序为 `'pre'` `(builtin)` `undefined` `'post'` */ order?: 'pre' | 'post'; /** * 不满足条件的提示 */ message: string; /** * 不满足条件的提示级别,仅 `'error'` 会导致参数无效,且后续约束不再检查 */ level?: ConstraintLevel; /** * 不满足条件的提示附加参数 */ messageData?: ExpressionSource | VmRecord | ArgumentFunction; /** * 约束条件 */ condition: ConditionExpression | ConditionFunction; } /** 检查是否为 Constraint */ export function isConstraint(value: unknown): value is Constraint { if (value == null || typeof value !== 'object') return false; if ('key' in value || 'name' in value || 'description' in value || 'items' in value) return false; const v = value as Constraint; if (typeof v.message != 'string') return false; if (typeof v.condition != 'function' && typeof v.condition != 'string') return false; if ( v.messageData != null && typeof v.messageData != 'string' && typeof v.messageData != 'object' && typeof v.messageData != 'function' ) return false; return true; }