Skip to content

Commit

Permalink
Fix input value for Decimal validate hook (#9262)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedybaird authored Sep 3, 2024
1 parent 4f8f925 commit d543e60
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-rocks-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": patch
---

Fixes `decimal` field bug (#8597) by parsing to `Decimal` before lessThan / greaterThan checks
11 changes: 9 additions & 2 deletions packages/core/src/fields/types/decimal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ function parseDecimalValueOption (meta: FieldData, value: string, name: string)
return decimal
}

export function decimal <ListTypeInfo extends BaseListTypeInfo>(config: DecimalFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
function safeParseDecimalValueOption (meta: FieldData, value: string | null | undefined, name: string): Decimal | null | undefined {
if (value === null || value === undefined) {
return value
}
return parseDecimalValueOption(meta, value, name)
}

export function decimal <ListTypeInfo extends BaseListTypeInfo> (config: DecimalFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
const {
isIndexed,
precision = 18,
Expand Down Expand Up @@ -107,7 +114,7 @@ export function decimal <ListTypeInfo extends BaseListTypeInfo>(config: DecimalF
} = makeValidateHook(meta, config, ({ resolvedData, operation, addValidationError }) => {
if (operation === 'delete') return

const value: Decimal | null | undefined = resolvedData[meta.fieldKey]
const value = safeParseDecimalValueOption(meta, resolvedData[meta.fieldKey], 'value')
if (value != null) {
if (min !== undefined && value.lessThan(min)) {
addValidationError(`value must be greater than or equal to ${min}`)
Expand Down

0 comments on commit d543e60

Please sign in to comment.