Skip to content
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

proof-of-concept(wip): allow custom validator #447

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jankapunkt
Copy link
Member

@jankapunkt jankapunkt commented Mar 21, 2024

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 (not 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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant