我一直在使用Folktale Validation进行新项目,但我发现它真的很有用,但我已经找到了需要顺序验证。我有一个配置对象,我需要执行以下验证:
每个验证都取决于之前的验证 - 如果项目不是一个对象,验证其键是无意义的(并且会出错),如果对象没有键,则验证它们的值是没有意义的。实际上,如果验证失败,我想要进行短路验证。
我最初的想法是使用Result代替Validatio , but mixing the two types feels confusing, and I already have
validateIsObject`定义并在别处使用。
我现在的(工作但丑陋)解决方案在这里:
import { validation } from 'folktale';
import { validateIsObject } from 'folktale-validations';
import validateConfigKeys from './validateConfigKeys';
import validateConfigValues from './validateConfigValues';
const { Success, Failure } = validation;
export default config => {
const wasObject = validateIsObject(config);
let errorMessages;
if (Success.hasInstance(wasObject)) {
const hadValidKeys = validateConfigKeys(config);
if (Success.hasInstance(hadValidKeys)) {
const hasValidValues = validateConfigValues(config);
if (Success.hasInstance(hasValidValues)) {
return Success(config);
}
errorMessages = hasValidValues.value;
} else {
errorMessages = hadValidKeys.value;
}
} else {
errorMessages = wasObject.value;
}
return Failure(errorMessages);
};
我最初采用嵌套matchWith
的方法,但这更难阅读。
如何改进此解决方案?
答案 0 :(得分:1)
您可以编写一个应用验证规则的帮助程序,直到返回Failure
为止。一个简单的例子:
const validateUntilFailure = (rules) => (x) => rules.reduce(
(result, rule) => Success.hasInstance(result)
? result.concat(rule(x))
: result,
Success()
);
我们使用concat
来合并两个结果。我们使用Success.hasInstance
来检查是否需要应用下一个规则。您的模块现在将是一行:
export default config => validateUntilFailure([
validateIsObject, validateConfigKeys, validateConfigValues
]);
请注意,此实现在看到Failure
后不会提前返回。递归实现可能是更具功能性的方法,但不会吸引所有人:
const validateUntilFailure = ([rule, ...rules], x, result = Success()) =>
Failure.hasInstance(result) || !rule
? result
: validateUntilFailure(rules, x, result.concat(rule(x)))
请查看以下示例以了解运行代码。注释掉的部分显示了如何运行所有规则,即使存在故障。
const { Success, Failure } = folktale.validation;
const validateIsObject = (x) =>
x !== null && x.constructor === Object
? Success(x)
: Failure(['Input is not an object']);
const validateHasRightKeys = (x) =>
["a", "b"].every(k => k in x)
? Success(x)
: Failure(['Item does not have a & b.']);
const validateHasRightValues = (x) =>
x.a < x.b
? Success(x)
: Failure(['b is larger or equal to a']);
// This doesn't work because it calls all validations on
// every item
/*
const validateItem = (x) =>
Success().concat(validateIsObject(x))
.concat(validateHasRightKeys(x))
.concat(validateHasRightValues(x))
.map(_ => x);
*/
// General validate until failure function:
const validateUntilFailure = (rules) => (x) => rules.reduce(
(result, rule) => Success.hasInstance(result)
? result.concat(rule(x))
: result,
Success()
);
// Let's try it out!
const testCases = [
null,
{ a: 1 },
{ b: 2 },
{ a: 1, b: 2 },
{ a: 2, b: 1 }
];
const fullValidation = validateUntilFailure([
validateIsObject,
validateHasRightKeys,
validateHasRightValues
]);
console.log(
testCases
.map(x => [x, fullValidation(x)])
.map(stringifyResult)
.join("\n")
);
function stringifyResult([input, output]) {
return `input: ${JSON.stringify(input)}, ${Success.hasInstance(output) ? "success:" : "error:"} ${JSON.stringify(output.value)}`;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/folktale/2.0.1/folktale.min.js"></script>
&#13;