shelving
Version:
Toolkit for using data in JavaScript.
27 lines (26 loc) • 909 B
JavaScript
import { getUUID } from "../util/uuid.js";
import { NULLABLE } from "./NullableSchema.js";
import { StringSchema } from "./StringSchema.js";
/**
* Type of `StringSchema` that defines a valid UUID (versions 1-5). Defaults to any-version validation.
* - Input is trimmed and lowercased.
* - Falsy values are converted to empty string.
*/
export class UUIDSchema extends StringSchema {
constructor({ one = "UUID", title = "UUID", ...rest } = {}) {
super({
one,
title,
...rest,
min: 32,
max: 36, // 36 chars including hyphens (which get stripped by sanitize for appearances).
});
}
sanitize(str) {
return getUUID(str) || "";
}
}
/** Any valid UUID (versions 1-5) */
export const UUID = new UUIDSchema({ title: "ID" });
/** Any valid UUID (versions 1-5) or null */
export const NULLABLE_UUID = NULLABLE(UUID);