'use strict';

var Ajv = require('../ajv');
require('../chai').should();


describe('validation options', function() {
  describe('format', function() {
    it('should not validate formats if option format == false', function() {
      var ajv = new Ajv
        , ajvFF = new Ajv({ format: false });

      var schema = { format: 'date-time' };
      var invalideDateTime = '06/19/1963 08:30:06 PST';

      ajv.validate(schema, invalideDateTime) .should.equal(false);
      ajvFF.validate(schema, invalideDateTime) .should.equal(true);
    });
  });


  describe('formats', function() {
    it('should add formats from options', function() {
      var ajv = new Ajv({ formats: {
        identifier: /^[a-z_$][a-z0-9_$]*$/i
      }});

      var validate = ajv.compile({ format: 'identifier' });

      validate('Abc1') .should.equal(true);
      validate('123') .should.equal(false);
      validate(123) .should.equal(true);
    });
  });

  describe('keywords', function() {
    it('should add keywords from options', function() {
      var ajv = new Ajv({ keywords: {
        string: {
          validate: function (schema, data ) {

            console.log(">>", data);
            return /^[a-z_$][a-z0-9_$]*$/i.test(data);
          }
        }
      }});

      var validate = ajv.compile({ string: true });

      validate('Abc1') .should.equal(true);
      validate('foo bar').should.equal(false);
      validate('123').should.equal(false);
      validate(123).should.equal(false);
      validate(123).should.equal(false);
    });
  });


  describe('uniqueItems', function() {
    it('should not validate uniqueItems with uniqueItems option == false', function() {
      testUniqueItems(new Ajv({ uniqueItems: false }));
      testUniqueItems(new Ajv({ uniqueItems: false, allErrors: true }));

      function testUniqueItems(ajv) {
        var validate = ajv.compile({ uniqueItems: true });
        validate([1,2,3]) .should.equal(true);
        validate([1,1,1]) .should.equal(true);
      }
    });
  });


  describe('unicode', function() {
    it('should use String.prototype.length with unicode option == false', function() {
      var ajvUnicode = new Ajv;
      testUnicode(new Ajv({ unicode: false }));
      testUnicode(new Ajv({ unicode: false, allErrors: true }));

      function testUnicode(ajv) {
        var validateWithUnicode = ajvUnicode.compile({ minLength: 2 });
        var validate = ajv.compile({ minLength: 2 });

        validateWithUnicode('😀') .should.equal(false);
        validate('😀') .should.equal(true);

        validateWithUnicode = ajvUnicode.compile({ maxLength: 1 });
        validate = ajv.compile({ maxLength: 1 });

        validateWithUnicode('😀') .should.equal(true);
        validate('😀') .should.equal(false);
      }
    });
  });


  describe('multipleOfPrecision', function() {
    it('should allow for some deviation from 0 when validating multipleOf with value < 1', function() {
      test(new Ajv({ multipleOfPrecision: 7 }));
      test(new Ajv({ multipleOfPrecision: 7, allErrors: true }));

      function test(ajv) {
        var schema = { multipleOf: 0.01 };
        var validate = ajv.compile(schema);

        validate(4.18) .should.equal(true);
        validate(4.181) .should.equal(false);

        schema = { multipleOf: 0.0000001 };
        validate = ajv.compile(schema);

        validate(53.198098) .should.equal(true);
        validate(53.1980981) .should.equal(true);
        validate(53.19809811) .should.equal(false);
      }
    });
  });
});

@@ -826,32 +826,80 @@
 ld.equal(true);%0A
+      validate('foo bar') .should.equal(false);%0A
       validate('
@@ -1124,17 +1124,47 @@
     
+identifier: %7B%0A          type: '
 string
-: %7B
+',
 %0A   
@@ -1210,46 +1210,8 @@
  ) %7B
-%0A%0A            console.log(%22%3E%3E%22, data);
 %0A   
@@ -1331,22 +1331,26 @@
 mpile(%7B 
-string
+identifier
 : true %7D
@@ -1422,16 +1422,17 @@
 oo bar')
+ 
 .should.
@@ -1466,16 +1466,17 @@
 e('123')
+ 
 .should.
@@ -1500,32 +1500,33 @@
    validate(123)
+ 
 .should.equal(fa
@@ -1527,53 +1527,11 @@
 ual(
-false);%0A      validate(123).should.equal(fals
+tru
 e);%0A

update option keywords test