export default function bigNumberLessThanOrEqualToAssert(threshold, { validateSignificantDigits = true } = {}) {
  /**
   * Optional peer dependencies.
   */

  const BigNumber = require('bignumber.js');

  BigNumber.DEBUG = !!validateSignificantDigits;

  /**
   * Extend `Assert` with `BigNumberAssert`.
   */

  const Assert = BaseAssert.extend({ BigNumber: BigNumberAssert });

  /**
   * Class name.
   */

  this.__class__ = 'BigNumberLessThanOrEqualTo';

  if (typeof threshold === 'undefined') {
    throw new Error('A threshold value is required.');
  }

  new Assert().BigNumber({ validateSignificantDigits }).validate(threshold);

  this.threshold = new BigNumber(threshold);

  /**
   * Validation algorithm.
   */

  this.validate = value => {
    new Assert().BigNumber({ validateSignificantDigits }).validate(value);

    try {
      const number = new BigNumber(value);

      if (!number.isLessThanOrEqualTo(this.threshold)) {
        throw new Error();
      }
    } catch (e) {
      const context = { threshold: this.threshold.toString() };

      if (e.message.startsWith('[BigNumber Error]')) {
        context.message = e.message;
      }

      throw new Violation(this, value, context);
    }

    return true;
  };

  return this;
}
Example #2
0
  this.validate = value => {
    if (!isString(value)) {
      throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
    }

    try {
      is.choice(choices).validate(value);
    } catch (e) {
      throw new Violation(this, value, e.violation);
    }

    return true;
  };
  this.validate = value => {
    if (!isString(value) && !isNumber(value)) {
      throw new Violation(this, value, { value: 'must_be_a_string_or_number' });
    }

    if (!numeric.test(value)) {
      throw new Violation(this, value, { value: 'must_be_numeric' });
    }

    try {
      is.ofLength(this.boundaries).validate(value);
    } catch (e) {
      throw new Violation(this, value, e.violation);
    }

    return true;
  };
  this.validate = value => {
    if (typeof value !== 'string') {
      throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
    }

    if (!validator.isEmail(value)) {
      throw new Violation(this, value);
    }

    try {
      is.ofLength({ max: 254 }).validate(value);
    } catch (e) {
      throw new Violation(this, value);
    }

    return true;
  };
/**
 * Module dependencies.
 */

import PhoneToken from '../../src/asserts/phone-token-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend Assert with `PhoneToken`.
 */

const Assert = BaseAssert.extend({ PhoneToken });

/**
 * Test `PhoneTokenAssert`.
 */

describe('PhoneTokenAssert', () => {
  it('should throw an error if the token is not a string', () => {
    [[], {}, 2].forEach(choice => {
      try {
        new Assert().PhoneToken().validate(choice);

        should.fail();
      } catch (e) {
        e.should.be.instanceOf(Violation);
        e.violation.value.should.equal('must_be_a_string');
      }
    });
/**
 * Module dependencies.
 */

import Iso3166CountryAssert from '../../src/asserts/iso-3166-country-assert';
import should from 'should';
import { Assert as BaseAssert, Validator, Violation } from 'validator.js';

/**
 * Extend `Assert` with `Iso3166CountryAssert`.
 */

const Assert = BaseAssert.extend({
  Iso3166Country: Iso3166CountryAssert
});

/**
 * Test `Iso3166CountryAssert`.
 */

describe('Iso3166CountryAssert', () => {
  it('should throw an error if the input value is not a string', () => {
    const choices = [[], {}, 123];

    choices.forEach(choice => {
      try {
        new Assert().Iso3166Country().validate(choice);

        should.fail();
      } catch (e) {
Example #7
0
/**
 * Module dependencies.
 */

import { Assert as BaseAssert, Violation } from 'validator.js';
import TotpToken from '../../src/asserts/totp-token-assert';
import should from 'should';

/**
 * Extend Assert with `TotpToken`.
 */

const Assert = BaseAssert.extend({ TotpToken });

/**
 * Test `TotpTokenAssert`.
 */

describe('TotpTokenAssert', () => {
  it('should throw an error if the token is not a string nor a number', () => {
    [[], {}].forEach(choice => {
      try {
        new Assert().TotpToken().validate(choice);

        should.fail();
      } catch (e) {
        e.should.be.instanceOf(Violation);
        e.violation.value.should.equal('must_be_a_string_or_number');
      }
    });
Example #8
0
/**
 * Module dependencies.
 */

import * as asserts from 'validator.js-asserts';
import { resolution } from '../enums';
import { Assert as BaseAssert, Violation } from 'validator.js';
import { pick, values } from 'lodash';

/**
 * Inject `Uri` and `EqualKeys` extra asserts.
 */

const is = BaseAssert.extend(pick(asserts, ['Uri', 'EqualKeys']));

/**
 * Resolution choices.
 */

const choices = values(resolution);

/**
 * Export `LogoAssert`.
 */

export default function logoAssert() {
  // Class name.
  this.__class__ = 'Logo';

  // Validation algorithm.
/**
 * Module dependencies.
 */

import DateDiffGreaterThanOrEqualToAssert from '../../src/asserts/date-diff-greater-than-or-equal-to-assert';
import should from 'should';
import sinon from 'sinon';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `DateDiffGreaterThanOrEqualToAssert`.
 */

const Assert = BaseAssert.extend({
  DateDiffGreaterThanOrEqualTo: DateDiffGreaterThanOrEqualToAssert
});

/**
 * Test `DateDiffGreaterThanOrEqualToAssert`.
 */

describe('DateDiffGreaterThanOrEqualToAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().DateDiffGreaterThanOrEqualTo();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import JsonAssert from '../../src/asserts/json-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `JsonAssert`.
 */

const Assert = BaseAssert.extend({
  Json: JsonAssert
});

/**
 * Test `JsonAssert`.
 */

describe('JsonAssert', () => {
  it('should throw an error if the input value is not valid JSON', () => {
    const choices = [[], '["foo":"bar"}'];

    choices.forEach(choice => {
      try {
        new Assert().Json().validate(choice);

        should.fail();
      } catch (e) {
/**
 * Module dependencies.
 */

import BigNumber from 'bignumber.js';
import BigNumberGreaterThanOrEqualToAssert from '../../src/asserts/big-number-greater-than-or-equal-to-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `BigNumberGreaterThanOrEqualToAssert`.
 */

const Assert = BaseAssert.extend({
  BigNumberGreaterThanOrEqualTo: BigNumberGreaterThanOrEqualToAssert
});

/**
 * Test `BigNumberGreaterThanOrEqualToAssert`.
 */

describe('BigNumberGreaterThanOrEqualToAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().BigNumberGreaterThanOrEqualTo();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import BigNumber from 'bignumber.js';
import BigNumberLessThanAssert from '../../src/asserts/big-number-less-than-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `BigNumberLessThanAssert`.
 */

const Assert = BaseAssert.extend({
  BigNumberLessThan: BigNumberLessThanAssert
});

/**
 * Test `BigNumberLessThanAssert`.
 */

describe('BigNumberLessThanAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().BigNumberLessThan();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import NullOrStringAssert from '../../src/asserts/null-or-string-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `NullOrStringAssert`.
 */

const Assert = BaseAssert.extend({
  NullOrString: NullOrStringAssert
});

/**
 * Test `NullOrStringAssert`.
 */

describe('NullOrStringAssert', () => {
  it('should throw an error if the input value is not a `null` or a string', () => {
    const choices = [[], {}, 123];

    choices.forEach(choice => {
      try {
        new Assert().NullOrString().validate(choice);

        should.fail();
      } catch (e) {
/**
 * Module dependencies.
 */

import DateAssert from '../../src/asserts/date-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `DateAssert`.
 */

const Assert = BaseAssert.extend({
  Date: DateAssert
});

/**
 * Test `DateAssert`.
 */

describe('DateAssert', () => {
  it('should throw an error if the input value is not a string or a date', () => {
    const choices = [[], {}, 123];

    choices.forEach(choice => {
      try {
        new Assert().Date().validate(choice);

        should.fail();
      } catch (e) {
/**
 * Module dependencies.
 */

import DateDiffGreaterThanAssert from '../../src/asserts/date-diff-greater-than-assert';
import should from 'should';
import sinon from 'sinon';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `DateDiffGreaterThanAssert`.
 */

const Assert = BaseAssert.extend({
  DateDiffGreaterThan: DateDiffGreaterThanAssert
});

/**
 * Test `DateDiffGreaterThanAssert`.
 */

describe('DateDiffGreaterThanAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().DateDiffGreaterThan();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import DateDiffLessThanOrEqualToAssert from '../../src/asserts/date-diff-less-than-or-equal-to-assert';
import should from 'should';
import sinon from 'sinon';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `DateDiffLessThanOrEqualToAssert`.
 */

const Assert = BaseAssert.extend({
  DateDiffLessThanOrEqualTo: DateDiffLessThanOrEqualToAssert
});

/**
 * Test `DateDiffLessThanOrEqualToAssert`.
 */

describe('DateDiffLessThanOrEqualToAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().DateDiffLessThanOrEqualTo();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import CountryOrCallingCode from '../../src/asserts/country-or-calling-code-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend Assert with `CountryOrCallingCode`.
 */

const Assert = BaseAssert.extend({ CountryOrCallingCode });

/**
 * Test `CountryOrCallingCodeAssert`.
 */

describe('CountryOrCallingCodeAssert', () => {
  it('should throw an error if the country calling code is not a string or a number', () => {
    [[], {}].forEach(choice => {
      try {
        new Assert().CountryOrCallingCode().validate(choice);

        should.fail();
      } catch (e) {
        e.should.be.instanceOf(Violation);
        e.violation.value.should.equal('must_be_a_string_or_number');
      }
    });
/**
 * Module dependencies.
 */

import DateDiffLessThanAssert from '../../src/asserts/date-diff-less-than-assert';
import should from 'should';
import sinon from 'sinon';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `DateDiffLessThanAssert`.
 */

const Assert = BaseAssert.extend({
  DateDiffLessThan: DateDiffLessThanAssert
});

/**
 * Test `DateDiffLessThanAssert`.
 */

describe('DateDiffLessThanAssert', () => {
  it('should throw an error if `threshold` is missing', () => {
    try {
      new Assert().DateDiffLessThan();

      should.fail();
    } catch (e) {
      e.message.should.equal('A threshold value is required.');
    }
/**
 * Module dependencies.
 */

import Signature from '../../src/asserts/signature-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend Assert with `Logo`.
 */

const Assert = BaseAssert.extend({ Signature });

/**
 * Test `Signature`.
 */

describe('SignatureAssert', () => {
  it('should throw an error if `key` is missing', () => {
    try {
      new Assert().Signature();

      should.fail();
    } catch (e) {
      e.should.be.instanceOf(Error);
      e.message.should.equal('Key is missing');
    }
  });
/**
 * Module dependencies.
 */

import InternationalBankAccountNumberAssert from '../../src/asserts/international-bank-account-number-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';

/**
 * Extend `Assert` with `InternationalBankAccountNumberAssert`.
 */

const Assert = BaseAssert.extend({
  InternationalBankAccountNumber: InternationalBankAccountNumberAssert
});

/**
 * Test `InternationalBankAccountNumberAssert`.
 */

describe('InternationalBankAccountNumberAssert', () => {
  it('should throw an error if the input value is not a string', () => {
    const choices = [[], {}];

    choices.forEach(choice => {
      try {
        new Assert().InternationalBankAccountNumber().validate(choice);

        should.fail();
      } catch (e) {
/**
 * Created by witmi on 13/05/2016.
 */
'use strict';
var is = require('validator.js').Assert;
var validator = require('validator.js');

var constraint = validator.constraint({
    gender: [is.notBlank(), is.NotNull(), is.Required()],
    name: {
        title: [is.notBlank(), is.NotNull(), is.Required()],
        first: [is.notBlank(), is.NotNull(), is.Required()],
        last: [is.notBlank(), is.NotNull(), is.Required()]
    },
    location: {
        street: [is.notBlank(), is.NotNull(), is.Required()],
        city: [is.notBlank(), is.NotNull(), is.Required()],
        state: [is.notBlank(), is.NotNull(), is.Required()],
        zip: is.GreaterThan(0)
    },

    email: [is.email(), is.NotNull(), is.Required()],
    username: [is.notBlank(), is.NotNull(), is.Required()],
    password: [is.notBlank(), is.NotNull(), is.Required()],
    salt: [is.notBlank(), is.NotNull(), is.Required()],
    md5: [is.notBlank(), is.NotNull(), is.Required()],
    sha1: [is.notBlank(), is.NotNull(), is.Required()],
    sha256: [is.notBlank(), is.NotNull(), is.Required()],
    registered: is.GreaterThan(0),
    dob: is.GreaterThan(0),
    
Example #22
0
/**
 * Module dependencies.
 */

import { Assert as BaseAssert, Violation } from 'validator.js';
import { locale } from '../../src/enums';
import { values } from 'lodash';
import Locale from '../../src/asserts/locale-assert';
import should from 'should';

/**
 * Extend Assert with `Locale`.
 */

const Assert = BaseAssert.extend({ Locale });

/**
 * Test `LocaleAssert`.
 */

describe('LocaleAssert', () => {
  it('should throw an error if the locale is not a string', () => {
    [[], {}, 123].forEach(locale => {
      try {
        new Assert().Locale().validate(locale);

        should.fail();
      } catch (e) {
        e.should.be.instanceOf(Violation);
        e.violation.value.should.equal('must_be_a_string');