From 43acea931ac4b3846604a40569d125b2ad7155af Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 23 May 2024 15:23:02 +0200 Subject: [PATCH] Ensure iso time config still work --- .../src/sdk/app/rows/tests/utils.spec.ts | 90 +++++++++++++++++++ packages/server/src/sdk/app/rows/utils.ts | 21 ++++- 2 files changed, 107 insertions(+), 4 deletions(-) diff --git a/packages/server/src/sdk/app/rows/tests/utils.spec.ts b/packages/server/src/sdk/app/rows/tests/utils.spec.ts index 4c44791135..55cdf9ea20 100644 --- a/packages/server/src/sdk/app/rows/tests/utils.spec.ts +++ b/packages/server/src/sdk/app/rows/tests/utils.spec.ts @@ -1,3 +1,4 @@ +import dayjs from "dayjs" import { FieldType, INTERNAL_TABLE_SOURCE_ID, @@ -241,5 +242,94 @@ describe("validate", () => { expect(output.errors).toEqual({ time: ["can't be blank"] }) }) }) + + describe("range", () => { + const table = getTable() + table.schema.time.constraints = { + presence: true, + datetime: { + earliest: "10:00", + latest: "15:00", + }, + } + + it.each(["10:00", "15:00", `10:${minute()}`, "12:34"])( + "should accept values in range (%s)", + async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(true) + } + ) + + it.each([ + "9:59:50", + `${generator.integer({ min: 0, max: 9 })}:${minute()}`, + ])("should reject values before range (%s)", async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(false) + expect(output.errors).toEqual({ + time: ["must be no earlier than 10:00"], + }) + }) + + it.each([ + "15:00:01", + `${generator.integer({ min: 16, max: 23 })}:${minute()}`, + ])("should reject values after range (%s)", async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(false) + expect(output.errors).toEqual({ + time: ["must be no later than 15:00"], + }) + }) + + describe("datetime ISO configs", () => { + const table = getTable() + + table.schema.time.constraints = { + presence: true, + datetime: { + earliest: dayjs().hour(10).minute(0).second(0).toISOString(), + latest: dayjs().hour(15).minute(0).second(0).toISOString(), + }, + } + + it.each(["10:00", "15:00", `12:${minute()}`])( + "should accept values in range (%s)", + async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(true) + } + ) + + it.each([ + "09:59:50", + `${generator.integer({ min: 0, max: 9 })}:${minute()}`, + ])("should reject values before range (%s)", async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(false) + expect(output.errors).toEqual({ + time: ["must be no earlier than 10:00"], + }) + }) + + it.each([ + "15:00:01", + `${generator.integer({ min: 16, max: 23 })}:${minute()}`, + ])("should reject values after range (%s)", async time => { + const row = { time } + const output = await validate({ table, tableId: table._id!, row }) + expect(output.valid).toBe(false) + expect(output.errors).toEqual({ + time: ["must be no later than 15:00"], + }) + }) + }) + }) }) }) diff --git a/packages/server/src/sdk/app/rows/utils.ts b/packages/server/src/sdk/app/rows/utils.ts index 19e8869494..23e1ab3e6b 100644 --- a/packages/server/src/sdk/app/rows/utils.ts +++ b/packages/server/src/sdk/app/rows/utils.ts @@ -242,11 +242,24 @@ function validateTimeOnlyField( let castedConstraints = cloneDeep(constraints) let earliest, latest + let easliestTimeString: string, latestTimeString: string if (castedConstraints.datetime?.earliest) { - earliest = stringTimeToDate(castedConstraints.datetime?.earliest) + easliestTimeString = castedConstraints.datetime.earliest + if (dayjs(castedConstraints.datetime.earliest).isValid()) { + easliestTimeString = dayjs(castedConstraints.datetime.earliest).format( + "HH:mm" + ) + } + earliest = stringTimeToDate(easliestTimeString) } if (castedConstraints.datetime?.latest) { - latest = stringTimeToDate(castedConstraints.datetime?.latest) + latestTimeString = castedConstraints.datetime.latest + if (dayjs(castedConstraints.datetime.latest).isValid()) { + latestTimeString = dayjs(castedConstraints.datetime.latest).format( + "HH:mm" + ) + } + latest = stringTimeToDate(latestTimeString) } if (earliest && latest && earliest.isAfter(latest)) { @@ -271,11 +284,11 @@ function validateTimeOnlyField( m ?.replace( castedConstraints.datetime?.earliest || "", - constraints.datetime?.earliest || "" + easliestTimeString || "" ) .replace( castedConstraints.datetime?.latest || "", - constraints.datetime?.latest || "" + latestTimeString || "" ) ) if (jsValidation) {