ciscoheat / sveltekit-superforms

Making SvelteKit forms a pleasure to use!
https://superforms.rocks
MIT License
2.23k stars 66 forks source link

Support for "raw" JSON schema #378

Closed Leftium closed 6 months ago

Leftium commented 7 months ago

Is your feature request related to a problem? Please describe. SuperForms uses JSON schema internally, but there is no way to pass SuperForms a "raw" JSON schema.

"Raw" JSON schemas are useful for two use cases:

Describe the solution you'd like A JSON schema adapter like:

import { jsonschema } from 'sveltekit-superforms/adapters'

Describe alternatives you've considered

Additional context Related discussion: https://discord.com/channels/1088090866649939990/1201800301607399515

ciscoheat commented 7 months ago

As this is very complicated, making ajv work with ESM and moduleResolution is the best bet. Could need some help with that.

Leftium commented 7 months ago

Ah, since SuperForms uses JSON schema internally, I thought a jsonschema adapter wouldn't be difficult to add... (I looked at the SuperForms code myself, but wasn't sure how to do it.)

making ajv work with ESM and moduleResolution is the best bet. Could need some help with that.

I'd like to help, but I'm not sure what the problem is:

Also there seem to be several other JSON schema libraries listed since I last investigated. Could adapters be added for any of those? Like Hyperjump JSV or djv?


Also, I did get formdata validation with ajv working outside of SuperForms (in real-time onchange events on the client). I'm just having trouble updating SuperForms $errors with the validation errors on the client side. I've only managed to get it to work inside the onUpdate() event (in SPA mode).

ciscoheat commented 7 months ago

Since it's not tree-shakable (by not being ESM-compatible), it's basically included as long as it exists in the project as a dependency, no matter what library you're using. So I can't really add it until it's thoroughly fixed in the library itself.

Hyperjump looks nice, and seems modern, so it looks like a candidate. Basically any adapter can be used as a base, but typebox could be suitable. Type inference is probably not available in Hyperjump, but you could use json-schema-to-ts for that, if it's tree-shakable. X) It can be a bit slow though, in my tests.

AKuederle commented 7 months ago

I am currently trying to implement something similar @Leftium. Basically, I building support for a form builder that defines its fields using json-schema directly. Having the option to directly pass "raw" JSON schema to superforms (or first class support a validation library that supports it) would be amazing! (Just wanted to add my +1 to this)

@Leftium would it be possible to share the workaround using ajv manually? Thanks in advance!

Leftium commented 7 months ago

@AKuederle

  1. Use a very minimal, basic, generic schema to satisfy SuperForms.
  2. Get any errors from const validate = ajv.compile(schema); validate(data)
  3. Format and feed these errors to SuperForms setError()

Note I got this to work with SuperForms SPA mode after submitting the form (in one of the events).

My feature that required ajv + dynamic json-schema ended up being dropped. Otherwise I would have probably just rendered the validation errors from ajv manually myself and not used SuperForms for validation.

AKuederle commented 7 months ago

@ciscoheat As mentioned on discord, I might look into implementing a new adapter that could support raw json-schema (not sure how easy type inference is going to be, but let's see).

It seems like you have implemented code for ajv at some point, but removed it from the repo. Is that still around? might be a good starting point.

ciscoheat commented 7 months ago

Yes, here's the version before it was removed: https://github.com/ciscoheat/sveltekit-superforms/blob/bd221dd44fa41dadd30965d050975eee067191df/src/lib/adapters/ajv.ts

ciscoheat commented 7 months ago

The Typebox adapter (latest version) should be useful for making it use the latest interface and helpers: https://github.com/ciscoheat/sveltekit-superforms/blob/main/src/lib/adapters/typebox.ts

sukeshpabolu commented 7 months ago

so, which adapter can we use for json schema validation?

ciscoheat commented 6 months ago

Just found https://github.com/sagold/json-schema-library which looks promising, but I don't know about any details.

noobmaster19 commented 6 months ago

Hi, am facing a roadblock on this issue as well. Any help needed to implement this?

ciscoheat commented 6 months ago

It's ready to merge, just need to find the time.

ciscoheat commented 6 months ago

Here is the validator being used, btw; https://github.com/ExodusMovement/schemasafe

ciscoheat commented 6 months ago

Added now with 2.13.0: https://superforms.rocks/get-started/json-schema