-
-
Notifications
You must be signed in to change notification settings - Fork 109
Add support for Zodv4 #447
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
base: master
Are you sure you want to change the base?
Conversation
Thank you for getting this started, I'm continuing where you left off. I've merged in the latest work in |
A beta had just been released so you can test. The aim is to support zod & ajv. Personally, I'd like to achieve zod support the most but I felt it's important to have another package so the implementation isn't too tight nor specific to zod & simple-schema. Backwards compatibility is a must for us with. There's little to no change to current users that's why we came up with idea of adapters where we wrap validation libraries and make them compatible to be used within current collection2 code. Either this or we had to rewrite how collection2 run validation internally and collection2 is too tightly couple with simple-schema where it creates a validation context to ensure data integrity.
In order to maintain a nice developer experience you don't have to utilize |
Can you recheck please? @jankapunkt @9Morello I think what remains now is two things:
|
I'm sort of meandering around cluelessly here, but I think for schemaDetectors and Zod 4, you can use the following documentation as a guide: https://v4.zod.dev/packages/core#schemas
that At the moment the draft/beta has the following for /**
* Determines if a schema is a Zod schema
* @param {Object} schema - The schema to check
* @returns {Boolean} True if the schema is a Zod schema
*/
export const isZodSchema = (schema) => {
return schema &&
typeof schema === 'object' &&
schema._def &&
schema.safeParse &&
schema.parse &&
typeof schema.safeParse === 'function' &&
typeof schema.parse === 'function';
}; Things like Likewise, (You have to look at the v4.0.0-beta tag under packages/core/src to see some of the implementation details since I didn't notice these documented yet in some sort of automatically generated reference doc) I need to go through some of the error handling stuff separately. Otherwise I think some helper functions could be implemented to act as a compatibility layer between Zod 3 and Zod 4. It'll be interesting to see if anything emerges in the broader NPM community for that role, or maybe it's worth seeing how a similar package handles the migration. |
Hello Fellas, |
@harryadel I'd like to bump the discussion on splitting again. I personally think it's good to make SimpleSchema a weak dependency or even remove at all as I personally think we can 100% make this injectable. If Zod4 is implemented from your end I would start with the injection pattern. This would cause users to use multiple packages but I also think this makes sense in the long run. Package structure would be:
plus one of the following
What do you think? |
@jankapunkt Hey Jan, thank you for chiming in. I do agree with you on making simple-schema a weak dependence and so is zod but I don't think the multiple Meteor packages is the way to go. Rather, dynamically detecting what's the available validation package and use it accordingly, maybe even allow multiple validation libraries to co-exist and validate based on whatever's is attached to the collection. You can even see in the test-app for zod, the package is installed in the test-app itself and not collection2! Managing multiple packages would be very tedious and draining, and we can dynamically import whatever files needed based on the validation libraries used within a project. // this is pseudo code
import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions';
if (!Packages[`aldeed:simple-schema`] or !checkNpmVersions(zod)) {
// please add one supported validation library
} |
But how to make sure this stays lightweight on client? |
@jankapunkt by dynamically importing files as is already done but we're going to make fancier. The current dynamic importing imports the entire collection2, now with the new adapters code we can also instruct the users to selectively import the needed files for their respective validation libraries to work. |
For what it's worth I like @jankapunkt's suggestion. I also saw this today and wondered if it would help ? |
We all know that this library is on the one hand crucial for modern Meteor Apps, at the same time it's still tightly coupled with SimpleSchema.
This PR is a current proof of concept (note it's work-in-progress status) to make it independent from Simple Schema.
The idea is to be non-breaking for SimpleSchema users but provide all necessary functionality for other validation libs, like zod, ajv etc.
This change requires users to register a "validator" object that implements certain methods. Consider the example for Simple Schema (also located in
tests/prepare.js
:The hard part is to extract all Simple-Schema-specific code into this validator without breaking things and at the same time get the abstraction done in a way it will smoothly work with other libs.
Current status: