keystone icon indicating copy to clipboard operation
keystone copied to clipboard

decimal field with min or max, resolveInput triggers error

Open Thinkscape opened this issue 2 years ago • 2 comments

Steps

  1. Have a list with a decimal field
list({
  fields: {
    lineAmount: decimal({
      validation: { isRequired: false, min: "0.0" },
      defaultValue: "0",
      precision: 10,
      scale: 2,
    }),
});
  1. Have a list resolveInput that changes the value:
list({
  hooks: {
    resolveInput: ({ resolvedData }) {
      return {
        ...resolvedData,
        lineItem: 666
      };
   }
});
  1. Send a create or update against 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 beforeOperation and mutating the resolvedData instance works around the problem.
  • Adding max to field config doesn't help.
  • Removing min max validation from field config works around it (but disables validation)
  • Returning number or string from resolveInput for the decimal field doesn't seem to change anything.

Thinkscape avatar Jun 01 '23 02:06 Thinkscape

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.

borisno2 avatar Jun 06 '23 23:06 borisno2

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 🤔

Thinkscape avatar Jun 07 '23 06:06 Thinkscape