javascriptmeteorecmascript-6simple-schema

conditionally suppress field validation for SimpleSchema during Bulk Insert


I am using Meteor 1.5 with MongoDB 3.2

I am using below Simple Schema to insert into Clients Collection.

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

export const Clients = new Mongo.Collection('Clients');

ClientsSchema = new SimpleSchema({
  "gstNo": {
    type: String,
    label: "GST No.",
    regEx: /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/,
    optional: true,
  },
  "mobile": {
    type: String,
    label: "Mobile 1",
    regEx: /^[789]\d{9}$/,
  }
});

Clients.attachSchema( ClientsSchema );

With simple QuickForm using Aldeed's AutoForm 6.0 it works great.

Problem Scenario:

I perform Bulk Insert as I have Clients with count 3000. I Parse the "valid" excel sheet and then using loop I tried inserting data but the operation fails when regex did not match for Mobile No.

Question:

How to suppress SimpleSchema field validation when I perform "bulk insert" for a collection which already has a SimpleSchema attached to it? Also I want customer to put any Mobile no during Bulk Insert as User might not know the regex.


Solution

  • According to documentation you just have to suppress validation during your bulk insert:

    Clients.insert(doc, { validate: false });
    

    Added:

    If you don't want to suppress the whole document validation, you can attach second schema to the same collection (with removed validation for particular field) and switch to it during bulk insert:

    Clients.attachSchema(anotherSchema, { selector: { type: 'trustedMobile' } });
    ...
    Clients.insert(doc, { selector: { type: 'trustedMobile' } });
    

    Documentation for using multiple schemas.