Skip to content

Commit

Permalink
Fix boolean and ranged values are always required (#31)
Browse files Browse the repository at this point in the history
* fix: boolean and range Values are always required
  • Loading branch information
Nils-Kolvenbach authored Jun 14, 2024
1 parent 2c21217 commit 25c34f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-kangaroos-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@elek-io/shared': patch
---

fix: boolean and range Values are always required
16 changes: 9 additions & 7 deletions src/valueSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export type NumberValueDefinition = z.infer<typeof numberValueDefinitionSchema>;
export const rangeValueDefinitionSchema =
NumberValueDefinitionBaseSchema.extend({
inputType: z.literal(ValueInputTypeSchema.Enum.range),
// Overwrite from optional to required because a range needs min, max and default to work
// Overwrite from optional to required because a range needs min, max and default to work and is required, since it always returns a number
isRequired: z.literal(true),
min: z.number(),
max: z.number(),
defaultValue: z.number(),
Expand All @@ -192,12 +193,13 @@ export type RangeValueDefinition = z.infer<typeof rangeValueDefinitionSchema>;
* Boolean based Values
*/

export const BooleanValueDefinitionBaseSchema = ValueDefinitionBaseSchema.omit({
isRequired: true,
}).extend({
valueType: z.literal(ValueTypeSchema.Enum.boolean),
defaultValue: z.boolean(),
});
export const BooleanValueDefinitionBaseSchema =
ValueDefinitionBaseSchema.extend({
valueType: z.literal(ValueTypeSchema.Enum.boolean),
// Overwrite from optional to required because a boolean needs a default to work and is required, since it always is either true or false
isRequired: z.literal(true),
defaultValue: z.boolean(),
});

export const toggleValueDefinitionSchema =
BooleanValueDefinitionBaseSchema.extend({
Expand Down
35 changes: 1 addition & 34 deletions test/valueSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Dynamic zod schema', () => {
},
defaultValue: true,
inputWidth: '12',
isRequired: true,
isDisabled: false,
});

Expand Down Expand Up @@ -131,40 +132,6 @@ describe('Dynamic zod schema', () => {
expect(requiredRangeValueschema.safeParse({}).success).toBe(false);
});

it('from optional range Value input type definition can be generated and parsed with', () => {
const optionalRangeValueschema = getValueContentSchemaFromDefinition({
id: uuid(),
valueType: 'number',
inputType: 'range',
label: {
en: 'Test',
},
description: {
en: 'Test',
},
min: 5,
max: 10,
defaultValue: 7,
inputWidth: '12',
isDisabled: false,
isRequired: false,
isUnique: false,
});

expect(optionalRangeValueschema.safeParse(5).success).toBe(true);
expect(optionalRangeValueschema.safeParse(10).success).toBe(true);
expect(optionalRangeValueschema.safeParse(7.5).success).toBe(true);
expect(optionalRangeValueschema.safeParse(undefined).success).toBe(true);

expect(optionalRangeValueschema.safeParse(4).success).toBe(false);
expect(optionalRangeValueschema.safeParse(11).success).toBe(false);
expect(optionalRangeValueschema.safeParse('').success).toBe(false);
expect(optionalRangeValueschema.safeParse(null).success).toBe(false);
expect(optionalRangeValueschema.safeParse(0).success).toBe(false);
expect(optionalRangeValueschema.safeParse([]).success).toBe(false);
expect(optionalRangeValueschema.safeParse({}).success).toBe(false);
});

it('from required text Value input type definition can be generated and parsed with', () => {
const requiredTextValueschema = getValueContentSchemaFromDefinition({
id: uuid(),
Expand Down

0 comments on commit 25c34f1

Please sign in to comment.