keystone
keystone copied to clipboard
decimal field with min or max, resolveInput triggers error
Steps
- Have a list with a decimal field
list({
fields: {
lineAmount: decimal({
validation: { isRequired: false, min: "0.0" },
defaultValue: "0",
precision: 10,
scale: 2,
}),
});
- Have a list
resolveInputthat changes the value:
list({
hooks: {
resolveInput: ({ resolvedData }) {
return {
...resolvedData,
lineItem: 666
};
}
});
- Send a
createorupdateagainst the list
Expected
Items gets created/updated successfully (with the lineItem: 666 value)
Actual
An error occured while running "validateInput".
- ListName.lineAmount.hooks.validateInput: val.lessThan is not a function
Workarounds
- Moving the logic to
beforeOperationand mutating theresolvedDatainstance works around the problem. - Adding
maxto field config doesn't help. - Removing
minmaxvalidation from field config works around it (but disables validation) - Returning
numberorstringfromresolveInputfor thedecimalfield doesn't seem to change anything.
This is most likely due to the Decimal validation expecting to be passed a decimal (.lessThan is not available on number or string).
I am assuming if you passed a Decimal into resolveInput validation would succeed for example:
list({
hooks: {
resolveInput: ({ resolvedData }) {
return {
...resolvedData,
lineItem: new Decimal(666)
};
}
});
One possible fix in Keystone, to support passing in a number or string into resolve input, would be to parse val in the decimal validation.
Ah! I shall try that.
If that's the case, then the confusion came from the fact, that beforeOperation is perfectly fine with passing number values, and it casts (wraps) them with Decimal 🤔