Closed Leftium closed 6 months ago
As this is very complicated, making ajv work with ESM and moduleResolution is the best bet. Could need some help with that.
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).
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.
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!
@AKuederle
const validate = ajv.compile(schema); validate(data)
setError()
Note I got this to work with SuperForms SPA mode after submitting the form (in one of the events).
setError
was available.$errors
store is writeable, but updating it didn't seem to work well (my updates were either overwritten or ignored).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.
@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.
Yes, here's the version before it was removed: https://github.com/ciscoheat/sveltekit-superforms/blob/bd221dd44fa41dadd30965d050975eee067191df/src/lib/adapters/ajv.ts
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
so, which adapter can we use for json schema validation?
Just found https://github.com/sagold/json-schema-library which looks promising, but I don't know about any details.
Hi, am facing a roadblock on this issue as well. Any help needed to implement this?
It's ready to merge, just need to find the time.
Here is the validator being used, btw; https://github.com/ExodusMovement/schemasafe
Added now with 2.13.0: https://superforms.rocks/get-started/json-schema
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
setError()
/$errors
Additional context Related discussion: https://discord.com/channels/1088090866649939990/1201800301607399515