How to use the simpl-schema.addValidator function in simpl-schema

To help you get started, we’ve selected a few simpl-schema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github reactioncommerce / reaction / client / modules / i18n / startup.js View on Github external
import { Template } from "meteor/templating";
import { $ } from "meteor/jquery";
import { Tracker } from "meteor/tracker";
import { ReactiveVar } from "meteor/reactive-var";
import SimpleSchema from "simpl-schema";
import { Reaction } from "/client/api";
import { Shops, Translations, Packages } from "/lib/collections";
import Schemas from "@reactioncommerce/schemas";
import i18next, { getLabelsFor, getValidationErrorMessages, i18nextDep, currencyDep } from "./main";
import { mergeDeep } from "/lib/api";

/**
 * Every schema that feature an expireMonth and an expireYear
 * field will be validated against the dateBeforeNow rule.
 */
SimpleSchema.addValidator(function () {
  let expireMonth;
  let expireYear;
  let sibling;
  if (this.key === "expireMonth") {
    sibling = "expireYear";
    expireMonth = this.value;
    expireYear = this.field(sibling).value;
  }
  if (this.key === "expireYear") {
    sibling = "expireMonth";
    expireMonth = this.field(sibling).value;
    expireYear = this.value;
  }
  if (expireYear && expireMonth) {
    const now = new Date();
    const expire = new Date(expireYear, expireMonth);