Ensure iso time config still work
This commit is contained in:
parent
b8400294d5
commit
43acea931a
|
@ -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"],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue