import { BaseSchema, Schema, SchemaType } from "../../Schema"; import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType"; import { maybeSkipValidation } from "../../utils/maybeSkipValidation"; import { list } from "../list"; import { getSchemaUtils } from "../schema-utils"; export function set(schema: Schema): Schema> { const listSchema = list(schema); const baseSchema: BaseSchema> = { parse: async (raw, opts) => { const parsedList = await listSchema.parse(raw, opts); if (parsedList.ok) { return { ok: true, value: new Set(parsedList.value), }; } else { return parsedList; } }, json: async (parsed, opts) => { if (!(parsed instanceof Set)) { return { ok: false, errors: [ { path: opts?.breadcrumbsPrefix ?? [], message: getErrorMessageForIncorrectType(parsed, "Set"), }, ], }; } const jsonList = await listSchema.json([...parsed], opts); return jsonList; }, getType: () => SchemaType.SET, }; return { ...maybeSkipValidation(baseSchema), ...getSchemaUtils(baseSchema), }; }