Hello fellas,
I’ve been working on something quietly for a while (I don’t like to make a fuss and then not follow through). I’ve been adding Zod support to collection2!
The implementation is now somewhat stable, but I really need your help testing it out. It’s based on the great work of @jkuester
You can try it with: aldeed:collection2@4.1.1-beta.3
Any feedback would be greatly appreciated. Thanks!
7 Likes
@harry97 sounds promising! Thanks for sharing.
Regarding types, does it support anything like Zod’s branded types for ids? In the past, when integrating Zod with Meteor collections, I’ve had to manually tweak the collection types to not get TypeScript errors when using normal TS strings and a branded type.
I believe for the most part yes but you can give it a go too and if you find any problems, I’ll gladly fix.
1 Like
@harry97 I see, thanks. I noticed you added tests for branded types after my comment, that was very mindful.
I made some comments on the PR on things I noticed, hope it can help.
Ah, you noticed?
Thank you for your comments, I’ll attend to them and let you know. I’ve been noticing your great Meteor PRs, glad you’re on the forums with us now
Please also discuss with us the future architecture: Collection2 + Zod Support (testers needed)
The more adapters we get the more it is important to think about things like complexity, dependencies, bundle size etc.
1 Like
Meteor-Community-Packages:master
← Meteor-Community-Packages:feature/inject-validation-lib
opened 10:09AM - 21 Mar 24 UTC
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`:
```js
import SimpleSchema from "meteor/aldeed:simple-schema";
Collection2.defineValidation({
name: 'SimpleSchema',
// check if the schema is a schema instance
is: schema => SimpleSchema.isSimpleSchema(schema),
// create a new schema instance by definition object
create: schema => new SimpleSchema(schema),
// extend the schema, I don't know if other libs support this...
extend: (s1, s2) => {
if (s2.version >= 2) {
const ss = new SimpleSchema(s1);
ss.extend(s2);
return ss;
} else {
return new SimpleSchema([s1, s2]);
}
},
// mutated cleaning of a doc or modifier, I don't know mofidier support in other libs
clean: ({ doc, modifier, schema, userId, isLocalCollection, type }) => {
const isModifier = !Collection2.isInsertType(type);
const target = isModifier ? modifier : doc;
schema.clean(target, {
mutate: true,
isModifier,
// We don't do these here because they are done on the client if desired
filter: false,
autoConvert: false,
removeEmptyStrings: false,
trimStrings: false,
extendAutoValueContext: {
isInsert: Collection2.isInsertType(type),
isUpdate: Collection2.isUpdateType(type),
isUpsert: Collection2.isUpdateType(type),
userId,
isFromTrustedCode: false,
docId: doc?._id,
isLocalCollection
}
})
},
// incomplete yet, will be added once it's implemented
validate: () => {},
// the idea is to freeze the validator to avoid any tampering or redefining
freeze: false
});
```
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:
- [ ] simple schema
- [ ] ajv
- [ ] yup
- [ ] zod
4.1.1-beta.4
is published, please try it out!
1 Like