vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
74 lines • 2.71 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["value", "path", "label", "options", "originalValue", "sync"];
import { mapValues } from "../../../utils/index.js";
import Ref from "../Reference.js";
import ValidationError from "../ValidationError.js";
export default function createValidation(config) {
function validate(_ref, cb) {
var value = _ref.value,
path = _ref.path,
label = _ref.label,
options = _ref.options,
originalValue = _ref.originalValue,
sync = _ref.sync,
rest = _objectWithoutPropertiesLoose(_ref, _excluded);
var name = config.name,
test = config.test,
params = config.params,
message = config.message;
var parent = options.parent,
context = options.context,
rootValue = options.rootValue;
function resolve(item) {
return Ref.isRef(item) ? item.getValue(value, parent, context, rootValue) : item;
}
function createError(overrides) {
if (overrides === void 0) {
overrides = {};
}
var nextParams = mapValues(_extends({
value: value,
originalValue: originalValue,
label: label,
path: overrides.path || path
}, params, overrides.params), resolve);
var error = new ValidationError(ValidationError.formatError(overrides.message || message, nextParams), value, nextParams.path, overrides.type || name);
error.params = nextParams;
return error;
}
var ctx = _extends({
path: path,
parent: parent,
type: name,
createError: createError,
resolve: resolve,
options: options,
originalValue: originalValue
}, rest);
if (!sync) {
try {
Promise.resolve(test.call(ctx, value, ctx)).then(function (validOrError) {
if (ValidationError.isError(validOrError)) cb(validOrError);else if (!validOrError) cb(createError());else cb(null, validOrError);
});
} catch (err) {
cb(err);
}
return;
}
var result;
try {
var _result;
result = test.call(ctx, value, ctx);
if (typeof ((_result = result) == null ? void 0 : _result.then) === 'function') {
throw new Error("Validation test of type: \"" + ctx.type + "\" returned a Promise during a synchronous validate. " + "This test will finish after the validate call has returned");
}
} catch (err) {
cb(err);
return;
}
if (ValidationError.isError(result)) cb(result);else if (!result) cb(createError());else cb(null, result);
}
validate.OPTIONS = config;
return validate;
}