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

Can't get instance to work #464

Closed
ma-r-s opened this issue Feb 29, 2024 · 1 comment
Closed

Can't get instance to work #464

ma-r-s opened this issue Feb 29, 2024 · 1 comment
Assignees
Labels
external Problem has external origin

Comments

@ma-r-s
Copy link

ma-r-s commented Feb 29, 2024

I'm currently migrating from zod to valibot but haven't been able to get instance to work.

Here is the original Zod code:

pictures: z
.instanceof(File, { message: 'Please upload a file.' })
.array()
.refine((files) => files.length !== 0, 'At least one image is required')
.refine(
	(files) => files.length <= MAX_NUM_PICTURES,
	`Maximum number of pictures is ${MAX_NUM_PICTURES}`
)
.refine(
	(files) => files.every((f) => sizeInMB(f.size) <= MAX_IMAGE_SIZE),
	'Max 4MB upload size.'
)
.refine(
	(files) => files.every((f) => ACCEPTED_IMAGE_TYPES.includes(f.type)),
	'Unsupported file type.'
)

And here is my attempted translation to Valibot:

pictures: array(
	instance(File, '', [
		mimeType(ACCEPTED_IMAGE_TYPES, 'Unsupported file type.'),
		custom((file) => sizeInMB(file.size) <= MAX_IMAGE_SIZE, 'Max 4MB upload size.')
	]),
	[
		custom((files) => files.length !== 0, 'At least one image is required'),
		custom(
			(files) => files.length <= MAX_NUM_PICTURES,
			`Maximum number of pictures is ${MAX_NUM_PICTURES}`
		)
	]
)

I get the following error

Error: Unsupported valibot schema: instance
    at assert (file:///C:/Users/mario/Escritorio/Unihouse/node_modules/@gcornut/valibot-json-schema/dist/index.mjs:21:11)
    at converter (file:///C:/Users/mario/Escritorio/Unihouse/node_modules/@gcornut/valibot-json-schema/dist/index.mjs:226:5)
    at object (file:///C:/Users/mario/Escritorio/Unihouse/node_modules/@gcornut/valibot-json-schema/dist/index.mjs:124:29)
    at converter (file:///C:/Users/mario/Escritorio/Unihouse/node_modules/@gcornut/valibot-json-schema/dist/index.mjs:227:23)
    at Module.toJSONSchema (file:///C:/Users/mario/Escritorio/Unihouse/node_modules/@gcornut/valibot-json-schema/dist/index.mjs:248:35)
    at valibotToJSONSchema (C:\Users\mario\Escritorio\Unihouse\node_modules\sveltekit-superforms\dist\adapters\valibot.js:13:12)
    at _valibot (C:\Users\mario\Escritorio\Unihouse\node_modules\sveltekit-superforms\dist\adapters\valibot.js:36:44)
    at Module.memoized (C:\Users\mario\Escritorio\Unihouse\node_modules\memoize-weak\lib\memoize.js:79:20)
    at load (C:\Users\mario\Escritorio\Unihouse\src\routes\(app)\publish\new\+page.server.js:12:29)
    at Module.load_server_data (C:\Users\mario\Escritorio\Unihouse\node_modules\@sveltejs\kit\src\runtime\server\page\load_data.js:61:41)

I've also tried a simpler test directly from the Valibot documentation:

pictures: instance(File, [
  mimeType(['image/jpeg', 'image/png']),
  maxSize(1024 * 1024 * 10),
]);

However, all attempts result in the same error. I even tried using just instance(File).

I'd appreciate any insights or assistance in resolving this issue. Thank you!

@ma-r-s ma-r-s changed the title Can get instance to work Can't get instance to work Feb 29, 2024
@ma-r-s ma-r-s changed the title Can't get instance to work Can't get instance to work Feb 29, 2024
@fabian-hiller
Copy link
Owner

Thank you creating this issue. It seems that this error is not related to Valibot. It is related to valibot-json-schema. Please create an issue there. Feel free to link me.

@fabian-hiller fabian-hiller self-assigned this Mar 1, 2024
@fabian-hiller fabian-hiller added the external Problem has external origin label Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external Problem has external origin
Projects
None yet
Development

No branches or pull requests

2 participants