Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix input value for Decimal validate hook #9262

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading