slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
61 lines (60 loc) • 1.92 kB
JavaScript
import * as stringify from "@slightning/anything-to-string";
import { betterToString } from "../../utils";
import { TypeValidateError } from "./type-validate-error";
export function validate(name, value, type) {
let message = null, result = false;
try {
result = type.validate(value);
}
catch (error) {
if (!(error instanceof TypeValidateError)) {
throw error;
}
message = error.message;
}
if (!result && message == null) {
message = `不能将 ${betterToString(value)} 分配给 ${typeToString(type)}`;
}
if (message != null) {
if (name != null) {
message = `${name}:${message}`;
}
throw new TypeValidateError(message, value, type);
}
}
export function typeToString(type) {
return stringify.default(type, {
rules: [
{
test(__data) {
return true;
},
prepare(data, config, context) {
var _a;
(_a = data.typeToStringPrepare) === null || _a === void 0 ? void 0 : _a.call(data, config, context);
},
toString(data, config, context) {
return data.typeToString(config, context);
}
}
]
});
}
export function inlineTypeToString(type) {
return stringify.default(type, {
rules: [
{
test(__data) {
return true;
},
prepare(data, config, context) {
var _a;
(_a = data.inlineTypeToStringPrepare) === null || _a === void 0 ? void 0 : _a.call(data, config, context);
},
toString(data, config, context) {
return data.inlineTypeToString(config, context);
}
}
]
});
}