Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type & $$type seem to be ambiguous in the documentation #348

Open
fugufish opened this issue Sep 8, 2024 · 1 comment
Open

type & $$type seem to be ambiguous in the documentation #348

fugufish opened this issue Sep 8, 2024 · 1 comment

Comments

@fugufish
Copy link

fugufish commented Sep 8, 2024

The documentaiton is ambiguous about the difference between `type and $$type. For example this test on enum:

	it("allows enums in a nested schema", () => {
		const check = v.compile({type: "object", value: {$$type: "enum", values: ["a", "b", "c"]}});

		expect(check({value: "a"})).toBe(true);
		expect(check({value: "z"})).toBe(false);
	})

fails with the following result:

Cannot read properties of undefined (reading 'join')

However this

	it("allows enums in a nested schema", () => {
		const check = v.compile({type: "object", value: {type: "enum", values: ["a", "b", "c"]}});

		expect(check({value: "a"})).toBe(true);
		expect(check({value: "z"})).toBe(false);
	})

falis with

Expected: true
Received: [{"actual": undefined, "field": "type", "message": "The 'type' field is required.", "type": "required"}]

this means that $$type and type are not interchangable. $$type triggers the enum rule to be run, but the schema of the rule is changed causing it to throw an exception (i have a fix for that if needed), but type will not cause the rule to be run at all.

What is the expected behavior?

@icebob
Copy link
Owner

icebob commented Sep 30, 2024

The $$type is a shorthand form for object validation schema.

Full schema:

const schema = {
	point: {
		type: "object",
		properties: {
			x: "number",
			y: "number",
		}
	},
}

Shorthand:

const schema = {
	point: {
		$$type: "object",
		x: "number",
		y: "number",
	},
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants