Giter Club home page Giter Club logo

Comments (14)

Leftium avatar Leftium commented on August 25, 2024 1

@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).

  • I couldn't get real-time validation to work. I couldn't see any "hooks" I could use where setError was available.
  • The $errors store is writeable, but updating it didn't seem to work well (my updates were either overwritten or ignored).
  • I did not try with form actions.

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.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

Leftium avatar Leftium commented on August 25, 2024

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:

  • Does SuperForms fail to compile if ajv is included?
    • I'm using ajv in my SvelteKit project without issue
  • Is it just the bundle size because code-shaking is not possible?
    • If it's just the bundle size, is it possible to split the ajv adapter from the rest of the package so the bundle size only affects ajv users? I think ajv support with a larger bundle size would be preferrable to no support at all.

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).

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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.

from sveltekit-superforms.

AKuederle avatar AKuederle commented on August 25, 2024

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!

from sveltekit-superforms.

AKuederle avatar AKuederle commented on August 25, 2024

@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.

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

sukeshpabolu avatar sukeshpabolu commented on August 25, 2024

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

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

noobmaster19 avatar noobmaster19 commented on August 25, 2024

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

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

ciscoheat avatar ciscoheat commented on August 25, 2024

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

from sveltekit-superforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.