Path: blob/main/tests/unit/schema-validation/object-super.test.ts
6451 views
/*1* object-super.test.ts2*3* Copyright (C) 2022 Posit Software, PBC4*5*/67import {8assertYamlValidationFails,9expectValidationError,10schemaFromString,11yamlValidationUnitTest,12} from "./utils.ts";13import {14readAndValidateYamlFromMappedString,15ValidationError,16} from "../../../src/core/lib/yaml-schema/validated-yaml.ts";17import { asMappedString } from "../../../src/core/lib/mapped-text.ts";18import { refSchema } from "../../../src/core/lib/yaml-schema/common.ts";19import { assert } from "testing/asserts";2021yamlValidationUnitTest("object-super-closed", async () => {22const _schema1 = schemaFromString(`23id: object-super-closed-124object:25closed: true26properties:27foo: string28required: all29`);30const schema2 = schemaFromString(`31id: object-super-closed-232object:33super:34resolveRef: object-super-closed-135closed: true36properties:37bar: string38required: all39`);4041const obj1 = `42bar: yeah43`;44const obj2 = `45foo: yeah46`;47const obj3 = `48bar: yeah49foo: yeah50`;5152await assertYamlValidationFails(async () => {53const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(54asMappedString(obj1),55schema2,56);57throw new ValidationError("This should reject", yamlValidationErrors);58}, (e) => {59return expectValidationError(e)60.toHaveLength(1)61.forSchemaPathToEndWith("required");62});63await assertYamlValidationFails(async () => {64const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(65asMappedString(obj2),66schema2,67);68throw new ValidationError("This should reject", yamlValidationErrors);69}, (e) => {70return expectValidationError(e)71.toHaveLength(1)72.forSchemaPathToEndWith("required");73});7475const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(76asMappedString(obj3),77schema2,78);79assert(yamlValidationErrors.length === 0);80});8182yamlValidationUnitTest("object-super", async () => {83const _schema1 = schemaFromString(`84id: test-schema-185object:86properties:87foo: string88`);89const schema2 = schemaFromString(`90id: test-schema-291object:92super:93resolveRef: test-schema-194properties:95bar: number96`);9798const obj = `99foo: 3100bar: "bar"101`;102103await assertYamlValidationFails(async () => {104const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(105asMappedString(obj),106schema2,107);108throw new ValidationError("This should reject", yamlValidationErrors);109}, (e) => {110return expectValidationError(e)111.forSchemaPathToEndWith("type", 0)112.forSchemaPathToEndWith("type", 1)113.toHaveLength(2);114});115});116117yamlValidationUnitTest("object-super-2", async () => {118const obj = `119website:120title: "Sippin' Yack and Talkin' Smack"121twitter-card:122image: [profile.jpg, 2, 3, false]123`;124try {125await readAndValidateYamlFromMappedString(126asMappedString(obj),127refSchema("project-config", ""),128);129} catch (e: any) {130return expectValidationError(e)131.forSchemaPathToEndWith("type")132.toHaveLength(1);133}134});135136137