From 25c34f1d1ef0d6fd90f75ba0ba3a5d8bc87d7170 Mon Sep 17 00:00:00 2001 From: Nils Kolvenbach Date: Fri, 14 Jun 2024 21:38:42 +0200 Subject: [PATCH] Fix boolean and ranged values are always required (#31) * fix: boolean and range Values are always required --- .changeset/tame-kangaroos-carry.md | 5 +++++ src/valueSchema.ts | 16 ++++++++------ test/valueSchema.test.ts | 35 +----------------------------- 3 files changed, 15 insertions(+), 41 deletions(-) create mode 100644 .changeset/tame-kangaroos-carry.md diff --git a/.changeset/tame-kangaroos-carry.md b/.changeset/tame-kangaroos-carry.md new file mode 100644 index 0000000..1aea37e --- /dev/null +++ b/.changeset/tame-kangaroos-carry.md @@ -0,0 +1,5 @@ +--- +'@elek-io/shared': patch +--- + +fix: boolean and range Values are always required diff --git a/src/valueSchema.ts b/src/valueSchema.ts index bb278c0..9af76c1 100644 --- a/src/valueSchema.ts +++ b/src/valueSchema.ts @@ -181,7 +181,8 @@ export type NumberValueDefinition = z.infer; 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(), @@ -192,12 +193,13 @@ export type RangeValueDefinition = z.infer; * 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({ diff --git a/test/valueSchema.test.ts b/test/valueSchema.test.ts index 56d5ebe..3f2f23f 100644 --- a/test/valueSchema.test.ts +++ b/test/valueSchema.test.ts @@ -15,6 +15,7 @@ describe('Dynamic zod schema', () => { }, defaultValue: true, inputWidth: '12', + isRequired: true, isDisabled: false, }); @@ -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(),