import { decodeSchema, EncodedSchema, findAllDefs, JSONSchema, jsonSchema, } from "@noya-app/noya-schemas"; import { expect, it } from "bun:test"; import { checkTypeSafe } from "../checkType"; const defs = findAllDefs(jsonSchema); const stringSchema: JSONSchema = { type: "string", default: "hello world", _kind: "String", }; it("validates a string schema", () => { expect(checkTypeSafe(stringSchema, jsonSchema, defs)).toBe(true); const invalidStringSchema = { type: "string", default: "hello world", }; expect(checkTypeSafe(invalidStringSchema, jsonSchema, defs)).toBe(false); }); it("decodes a string schema", () => { const decoded = decodeSchema(stringSchema as unknown as EncodedSchema); const value = "hello world"; expect(checkTypeSafe(value, decoded, [])).toBe(true); const invalidValue = 42; expect(checkTypeSafe(invalidValue, decoded, [])).toBe(false); }); const arraySchema: JSONSchema = { type: "array", _kind: "Array", items: { type: "string", _kind: "String", }, }; it("validates an array schema", () => { expect(checkTypeSafe(arraySchema, jsonSchema, defs)).toBe(true); }); it("decodes an array schema", () => { const decoded = decodeSchema(arraySchema as unknown as EncodedSchema); const value = ["hello world"]; expect(checkTypeSafe(value, decoded, [])).toBe(true); const invalidValue = [42]; expect(checkTypeSafe(invalidValue, decoded, [])).toBe(false); }); const objectSchema: JSONSchema = { type: "object", _kind: "Object", properties: { name: { type: "string", _kind: "String", }, age: { type: "number", minimum: 0, _kind: "Number", }, tags: { type: "array", _kind: "Array", items: { type: "string", _kind: "String", }, }, }, required: ["name"], }; it("validates a nested object schema", () => { expect(checkTypeSafe(objectSchema, jsonSchema, defs)).toBe(true); }); it("decodes a nested object schema", () => { const decoded = decodeSchema(objectSchema as unknown as EncodedSchema); const value = { name: "hello world", age: 42, tags: ["hello", "world"] }; expect(checkTypeSafe(value, decoded, [])).toBe(true); const invalidValue = { // missing name age: 42, tags: ["hello", "world"], }; expect(checkTypeSafe(invalidValue, decoded, [])).toBe(false); const invalidValue2 = { name: "hello world", age: 42, // bad type tags: ["hello", "world", 42], }; expect(checkTypeSafe(invalidValue2, decoded, [])).toBe(false); });