Skip to content

Commit 2fd98cc

Browse files
authored
Fix/unify number refinements (#610)
* fix: unify number type handling and remove int references across components * fix: unify number type handling and remove int references across components
1 parent 4f56976 commit 2fd98cc

5 files changed

Lines changed: 53 additions & 81 deletions

File tree

.changeset/smart-areas-feel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-app/vue-components": patch
3+
---
4+
5+
fix: unify number type handling and remove 'int' references across components

packages/vue-components/src/components/OmegaForm/OmegaFormStuff.ts

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ export type StringFieldMeta = BaseFieldMeta & {
246246
}
247247

248248
export type NumberFieldMeta = BaseFieldMeta & {
249-
type: "number" | "int"
249+
type: "number"
250250
minimum?: number
251251
maximum?: number
252252
exclusiveMinimum?: number
253253
exclusiveMaximum?: number
254+
refinement?: "int"
254255
}
255256

256257
export type SelectFieldMeta = BaseFieldMeta & {
@@ -682,7 +683,8 @@ export const createMeta = <T = any>(
682683
// if this is S.Int (a refinement), set the type and skip following "from"
683684
// otherwise we'd lose the "Int" information and get "number" instead
684685
if (titleType === "Int" || titleType === "int") {
685-
meta["type"] = "int"
686+
meta["type"] = "number"
687+
meta["refinement"] = "int"
686688
// don't follow "from" for Int refinements
687689
} else if ("from" in property) {
688690
return createMeta<T>({
@@ -888,66 +890,28 @@ export const generateInputStandardSchemaFromFieldMeta = (
888890
}
889891
break
890892

891-
case "int": {
892-
// create a custom integer schema with translations
893-
// S.Number with empty message, then S.int with integer message
894-
schema = S
895-
.Number
896-
.annotations({
897-
message: () => trans("validation.empty")
898-
})
899-
.pipe(
900-
S.int({ message: (issue) => trans("validation.integer.expected", { actualValue: String(issue.actual) }) })
901-
)
902-
if (typeof meta.minimum === "number") {
903-
schema = schema.pipe(S.greaterThanOrEqualTo(meta.minimum)).annotations({
904-
message: () =>
905-
trans(meta.minimum === 0 ? "validation.number.positive" : "validation.number.min", {
906-
minimum: meta.minimum,
907-
isExclusive: true
908-
})
909-
})
910-
}
911-
if (typeof meta.maximum === "number") {
912-
schema = schema.pipe(S.lessThanOrEqualTo(meta.maximum)).annotations({
913-
message: () =>
914-
trans("validation.number.max", {
915-
maximum: meta.maximum,
916-
isExclusive: true
917-
})
918-
})
919-
}
920-
if (typeof meta.exclusiveMinimum === "number") {
921-
schema = schema.pipe(S.greaterThan(meta.exclusiveMinimum)).annotations({
922-
message: () =>
923-
trans(meta.exclusiveMinimum === 0 ? "validation.number.positive" : "validation.number.min", {
924-
minimum: meta.exclusiveMinimum,
925-
isExclusive: false
926-
})
927-
})
928-
}
929-
if (typeof meta.exclusiveMaximum === "number") {
930-
schema = schema.pipe(S.lessThan(meta.exclusiveMaximum)).annotations({
931-
message: () =>
932-
trans("validation.number.max", {
933-
maximum: meta.exclusiveMaximum,
934-
isExclusive: false
935-
})
936-
})
937-
}
938-
break
939-
}
940-
941893
case "number":
942-
schema = S.Number.annotations({
943-
message: () => trans("validation.number.expected", { actualValue: "NaN" })
944-
})
945-
946-
if (meta.required) {
947-
schema.annotations({
948-
message: () => trans("validation.empty")
894+
if (meta.refinement === "int") {
895+
schema = S
896+
.Number
897+
.annotations({
898+
message: () => trans("validation.empty")
899+
})
900+
.pipe(
901+
S.int({ message: (issue) => trans("validation.integer.expected", { actualValue: String(issue.actual) }) })
902+
)
903+
} else {
904+
schema = S.Number.annotations({
905+
message: () => trans("validation.number.expected", { actualValue: "NaN" })
949906
})
907+
908+
if (meta.required) {
909+
schema.annotations({
910+
message: () => trans("validation.empty")
911+
})
912+
}
950913
}
914+
951915
if (typeof meta.minimum === "number") {
952916
schema = schema.pipe(S.greaterThanOrEqualTo(meta.minimum)).annotations({
953917
message: () =>

packages/vue-components/src/components/OmegaForm/OmegaInputVuetify.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@
8181
</v-textarea>
8282
<component
8383
:is="inputProps.type === 'range' ? 'v-slider' : 'v-text-field'"
84-
v-if="inputProps.type === 'number' || inputProps.type === 'int' || inputProps.type === 'range'"
84+
v-if="inputProps.type === 'number' || inputProps.type === 'range'"
8585
:id="inputProps.id"
8686
:required="inputProps.required"
8787
:min="inputProps.min"
8888
:max="inputProps.max"
89-
:type="inputProps.type === 'int' ? 'number' : inputProps.type"
89+
:type="inputProps.type"
9090
:name="field.name"
9191
:label="inputProps.label"
9292
:error-messages="inputProps.errorMessages"

packages/vue-components/src/components/OmegaForm/OmegaInternalInput.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ const inputProps: ComputedRef<InputProps<From, Name>> = computed(() => ({
162162
required: isRequired.value,
163163
minLength: props.meta?.type === "string" && props.meta?.minLength,
164164
maxLength: props.meta?.type === "string" && props.meta?.maxLength,
165-
max: (props.meta?.type === "number" || props.meta?.type === "int")
165+
max: (props.meta?.type === "number")
166166
&& (props.meta?.maximum
167167
?? (typeof props.meta?.exclusiveMaximum === "number" && props.meta.exclusiveMaximum - 1)),
168-
min: (props.meta?.type === "number" || props.meta?.type === "int")
168+
min: (props.meta?.type === "number")
169169
&& (props.meta?.minimum
170170
?? (typeof props.meta?.exclusiveMinimum === "number" && props.meta.exclusiveMinimum + 1)),
171171
errorMessages: errors.value,

packages/vue-components/stories/OmegaForm/SimpleForm.vue

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
11
<template>
2-
<stocazzo.Form :subscribe="['values']">
2+
<form.Form :subscribe="['values']">
33
<template #default="{ subscribedValues: { values } }">
44
{{ values }}
5-
<stocazzo.TaggedUnion
5+
<form.TaggedUnion
66
label="select"
77
:options="[{
8-
title: 'cazzoPippo',
9-
value: 'cazzoPippo'
8+
title: 'one',
9+
value: 'one'
1010
}, {
11-
title: 'Pippocazzo',
12-
value: 'Pippocazzo'
11+
title: 'two',
12+
value: 'two'
1313
}]"
1414
>
15-
<stocazzo.Input name="a.height" />
16-
<stocazzo.Input name="a.width" />
17-
<template #Pippocazzo>
18-
<stocazzo.Input name="a.y" />
15+
<form.Input name="a.number" />
16+
<form.Input name="a.height" />
17+
<form.Input name="a.width" />
18+
<template #two>
19+
<form.Input name="a.y" />
1920
</template>
20-
<template #cazzoPippo>
21-
<stocazzo.Input name="a.z" />
21+
<template #one>
22+
<form.Input name="a.z" />
2223
</template>
2324
<v-btn type="submit">
2425
ciao
2526
</v-btn>
26-
</stocazzo.TaggedUnion>
27-
<stocazzo.Errors />
27+
</form.TaggedUnion>
28+
<form.Errors />
2829
</template>
29-
</stocazzo.Form>
30+
</form.Form>
3031
</template>
3132

3233
<script setup lang="ts">
3334
import { S } from "effect-app"
3435
import { useOmegaForm } from "../../src/components/OmegaForm"
3536
36-
const stocazzo = useOmegaForm(
37+
const form = useOmegaForm(
3738
S.Union(
3839
S.Struct({
3940
a: S.Struct({
41+
number: S.Number.pipe(S.int()).pipe(S.between(1, 20)),
4042
height: S.NonEmptyString100.pipe(S.minLength(10)),
4143
width: S.NonEmptyString100.pipe(S.minLength(10)),
4244
z: S.NonEmptyString100.pipe(S.minLength(10))
4345
}),
44-
_tag: S.Literal("cazzoPippo")
46+
_tag: S.Literal("one")
4547
}),
4648
S.Struct({
4749
a: S.Struct({
50+
number: S.Number.pipe(S.int()).pipe(S.between(1, 20)),
4851
height: S.NonNegativeInt.pipe(S.greaterThan(11)),
4952
width: S.NonNegativeInt.pipe(S.greaterThan(11)),
5053
y: S.NonNegativeInt.pipe(S.greaterThan(11))
5154
}),
52-
_tag: S.Literal("Pippocazzo")
55+
_tag: S.Literal("two")
5356
})
5457
)
5558
)

0 commit comments

Comments
 (0)