longshotlabs / simpl-schema

A JavaScript schema validation package that supports direct validation of MongoDB update modifier objects
https://www.npmjs.com/package/simpl-schema
MIT License
559 stars 114 forks source link

doc-validation without context #301

Open Sebl29 opened 6 years ago

Sebl29 commented 6 years ago

Hi aldeed,

I am adding a doc-validator with mySchema.addDocValidator(callbackFunction) and suffering under the circumstances, that I don't have an appropriate context (but window-context in the client). I would expect to have a context like on #addValidator, to check whether I am on insert/update/updateSet/delete.

My usecase is currently to re-use a set of allow/deny instructions for the whole document, in the same way I am already doing fieldwise and also in meteor-methods.

https://github.com/aldeed/simple-schema-js/blob/6213070e0e1bcb8b1e2cb515cba51dc3b5abfeb6/package/lib/doValidation.js#L320 const errors = func(obj);

As far as I see in the debugger, this is the line where the context gets lost. I would guess, that you already might have a function to get all needed context-parameters for the addValidator method. It would be nice to have the same context, so same possibilities, in both methods (doc-wise and field-wise). Thanks in advance,

awatson1978 commented 6 years ago

Hmmm... I'm having trouble with this line of code also, but for entirely different reasons. I'm trying to extend a schema like the following:

import SimpleSchema from 'simpl-schema';
import { BaseSchema } from 'meteor/clinical:hl7-resource-datatypes'

MedicationStatementSchema = new SimpleSchema({
  "resourceType" : {
    type: String,
    defaultValue: "MedicationStatement"
  },
  "foo" : {
    optional: true,
    type:  Object
    }
}
MedicationStatementSchema.extend(BaseSchema);

And it's kickingup a Uncaught TypeError: func is not a function error, which points to that same line (I think).

screen shot 2018-09-05 at 2 12 40 am

Drill into the error, and there's the const errors = func(obj); line.

screen shot 2018-09-05 at 2 11 37 am