hyperborea-character-sheet/src/sumOf.cjs

37 lines
888 B
JavaScript
Raw Normal View History

2022-04-26 22:55:59 +00:00
const _ = require("lodash");
2022-04-27 00:15:42 +00:00
const resolvePointer = require('./resolvePointer.cjs');
2022-04-26 22:55:59 +00:00
module.exports = (ajv) =>
ajv.addKeyword({
keyword: "sumOf",
validate: function validate(
{ list, map },
total,
_parent,
{ rootData, instancePath }
) {
if (list.$data) {
list = resolvePointer(rootData, instancePath, list.$data);
}
2022-04-27 00:15:42 +00:00
if (map) list = _.map(list, map);
2022-04-26 22:55:59 +00:00
if (_.sum(list) === total) return true;
validate.errors = [
{
keyword: "sumOf",
message: "should add up to sum total",
params: {
list,
},
},
];
return false;
},
$data: true,
errors: true,
});