1

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.

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81

1 Answers1

1

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.

Styx
  • 9,863
  • 8
  • 43
  • 53
  • Do you even take rest/sleep/go away, I find you very actively participating on SO LOL. Just kidding!. Btw let me test the answer. But it seems the answers will apply. – Ankur Soni Sep 15 '17 at 11:17
  • The same could be said about you :) Btw, I've updated my answer to add another possible solution. – Styx Sep 15 '17 at 11:19
  • Will it work for `collection2-core 2.0.1`? I am using latest `Autoform 6.2.0`. and the build for it is failing – Ankur Soni Sep 15 '17 at 11:33
  • It should. Why the build is failing though? – Styx Sep 15 '17 at 11:53
  • not my build, but there build. (see on the top of the page.) – Ankur Soni Sep 15 '17 at 11:55
  • If I attach 4 schemas to a Collection, which shall be referred by quickForm ? – Ankur Soni Sep 15 '17 at 12:28
  • 1
    I reckon, the schema you've specified in `schema="..."`. I don't see your template, so can't answer better, sorry. – Styx Sep 15 '17 at 12:51
  • i don't attach any schema there, I just give collection name, so any idea? e.g. `{{> quickForm id="insertDairyForm" collection=diary type="insert" doc=this omitFields="userId,createdAt,check"}}` – Ankur Soni Sep 15 '17 at 12:58
  • You can add `schema=...` along with `collection=diary`, I think it should work this way. – Styx Sep 15 '17 at 13:10