-
-
Notifications
You must be signed in to change notification settings - Fork 491
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
Feat: (proposal) prop validation. #271
Conversation
…eView in createBlockSpec)
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Looks interesting! It seems like using zod internally makes sense to ensure a strict schema, though one thing I'm a bit concerned about is exposing the zod API to consumers. I think ideally, you should be able to declare a prop schema using a vanilla TypeScript object. I'm not familiar with zod myself, but do you think it would be possible to enforce strict typing without having to declare the prop schema using zod functions? |
Fair point. I think that the approach of TRPC makes sense for this case: https://trpc.io/docs/server/validators#the-most-basic-validator-a-function. It enables the posibility to use various validators: simple function, zod schema, yup and others. It will depend on the user what they would like to use. Their implementation seems to be pretty straightforward:
What do you think about this approach? |
Hi @leweyse, Thanks for your work on this. Impressive work! I have some thoughts / questions on this. Want to make sure we have the picture clear before deciding on a direction :) Understanding the motivation
Unknowns re. complex prop types
Hope this makes sense. Let me know if you have any questions on the above! |
Hi @YousefED, Thanks for your input. Understanding the motivation
Unknowns re. complex prop types
It's just an idea, but those attribute names are valid (source).
Let me know what you think, I'll be glad to continue working on these changes. |
Clear! Thanks. Based on your input I suggest the following:
If validation is not the main goal, and the types can be enforced by raw Typescript, then I think we should focus on doing that without Zod. We can solve the runtime validation issue separately - imo better to address one problem at a time.
I'm still not sure about this. The example you give is about serializing to / from DOM, but perhaps PM and y-pm do work well with more complex attrs (except for the serialization). Have you tested this (separate from the serialization)? Re your proposal; if we want to serialize more complex props to / from DOM, I think it would be easier (from a maintainability perspective) to embed them as serialize / deserialize as raw JSON then inventing a new structure tbh |
Agreed that it would be nice to have support for runtime validation. Ideally, we get an internal Blocknote schema to handle this which maps 1:1 with types. In lieu of that I have written a basic schema based on the documented Blocknote schema
Note that the schema is generalized, and so types will not match 1:1 with the Blocknote type definitions |
Hi @matthewlipski. I've been working on a proposal for the propSchema validation, and I hope I can get your feedback. This is still a work in progress, so I'll be happy to discuss any suggestions or changes. This will also fix #264 .
I think the best fit for schema validation and type inference is zod. So, I worked on an implementation for the
propSchema
definition using zod. This is how it would look like: https://github.com/Leweyse/BlockNote/blob/feat/prop-validation/examples/editor/src/App.tsx#L10. This will make possible to parse the props values and also enable autocomplete when we use helpers asupdateBlock
orinsertBlocks
(example here). It will also simplify the type definition from the library, but keeping the app type-safe: https://github.com/Leweyse/BlockNote/blob/feat/prop-validation/packages/core/src/extensions/Blocks/api/blockTypes.ts#L45.