shelving
Version:
Toolkit for using data in JavaScript.
37 lines (36 loc) • 1.73 kB
JavaScript
import { ValueFeedback } from "../feedback/Feedback.js";
import { roundStep } from "../util/number.js";
import { Time, getTime } from "../util/time.js";
import { OPTIONAL } from "./OptionalSchema.js";
import { Schema } from "./Schema.js";
/** Define a valid time in 24h hh:mm:ss.fff format, e.g. `23:59` or `24:00 */
export class TimeSchema extends Schema {
min;
max;
/**
* Rounding step (in milliseconds, because that's the base unit for time), e.g. `60000` will round to the nearest second.
* - Note: `<input type="time">` elements expect `step=""` to be in _seconds_ so you need to multiply this by `1000`
*/
step;
constructor({ min, max, step = 60, title = "Time", value = "now", ...options }) {
super({ title, value, ...options });
this.min = getTime(min);
this.max = getTime(max);
this.step = step;
}
validate(unsafeValue = this.value) {
const optionalTime = getTime(unsafeValue);
if (!optionalTime)
throw new ValueFeedback(unsafeValue ? "Invalid time" : "Required", unsafeValue);
const roundedTime = typeof this.step === "number" ? new Time(roundStep(optionalTime.time, this.step)) : optionalTime;
if (this.max && roundedTime > this.max)
throw new ValueFeedback(`Maximum ${this.max.format()}`, roundedTime);
if (this.min && roundedTime < this.min)
throw new ValueFeedback(`Minimum ${this.min.format()}`, roundedTime);
return roundedTime.long;
}
}
/** Valid time, e.g. `2005-09-12` (required because falsy values are invalid). */
export const TIME = new TimeSchema({});
/** Valid time, e.g. `2005-09-12`, or `null` */
export const OPTIONAL_TIME = OPTIONAL(TIME);