/* eslint-env mocha */
////////////////////////////////////////////////////////////////////////////////
// REQUIRES : BEGIN
////////////////////////////////////////////////////////////////////////////////

import { assert } from 'chai';

import { validate } from '../src/index';
import { Presence } from '../src/enums';

////////////////////////////////////////////////////////////////////////////////
// REQUIRES : END
////////////////////////////////////////////////////////////////////////////////

const MY_CUSTOM_TYPE = 'MY_CUSTOM_TYPE';

describe('Custom casts:', function () {

  const scheme = {
    type : MY_CUSTOM_TYPE,
    $presence : Presence.REQUIRED
  };

  it('should throw if custom cast is not registered', function () {
    assert.throws(() => validate(5, scheme), 'Unknown type MY_CUSTOM_TYPE');
  });

  it('should use custom cast if it\'s provided', function () {

    const options = {
      casts : {
        [MY_CUSTOM_TYPE] (v) {
          return v + 1;
        }
      }
    };

    const result = validate(5, scheme, options);
    assert.equal(result, 6);
  });

  it('should use custom strict cast if it\'s provided', function () {

    const options = {
      strict : true,
      castsStrict : {
        [MY_CUSTOM_TYPE] (v) {
          return v + 1;
        }
      }
    };

    const result = validate(5, scheme, options);
    assert.equal(result, 6);
  });

});

@@ -199,16 +199,114 @@
 //////%0A%0A
+import isNaN from 'lodash-compat/lang/isNaN';%0Aimport isNumber from 'lodash-compat/lang/isNumber';%0A
 import %7B
@@ -378,16 +378,28 @@
 import %7B
+ CAST_ERROR,
  Presenc
@@ -615,40 +615,26 @@
 nst 
-MY_CUSTOM_TYPE = 'MY_CUSTOM_TYPE
+INTEGER = 'INTEGER
 ';%0A%0A
@@ -704,30 +704,23 @@
  type : 
-MY_CUSTOM_TYPE
+INTEGER
 ,%0A    $p
@@ -880,30 +880,23 @@
 wn type 
-MY_CUSTOM_TYPE
+INTEGER
 ');%0A  %7D)
@@ -1005,38 +1005,31 @@
  %7B%0A        %5B
-MY_CUSTOM_TYPE
+INTEGER
 %5D (v) %7B%0A    
@@ -1038,66 +1038,287 @@
     
-return v + 1;%0A        %7D%0A      %7D%0A    %7D;%0A%0A    const result
+v = v - 0;%0A          if (isNumber(v) && !isNaN(v) && v === Math.round(v))%0A            return v;%0A          return CAST_ERROR;%0A        %7D%0A      %7D%0A    %7D;%0A%0A    assert.equal(validate(5, scheme, options), 5);%0A    assert.equal(validate('5', scheme, options), 5);%0A%0A    assert.throws(()
  =
+%3E
  val
@@ -1316,32 +1316,34 @@
 () =%3E validate(5
+.5
 , scheme, option
@@ -1348,36 +1348,159 @@
 ons)
-;%0A    assert.equal(result, 6
+,%0A      '%D1%81ouldn%5C't cast value to type INTEGER');%0A    assert.throws(() =%3E validate('5.5', scheme, options),%0A      '%D1%81ouldn%5C't cast value to type INTEGER'
 );%0A 
@@ -1654,22 +1654,15 @@
    %5B
-MY_CUSTOM_TYPE
+INTEGER
 %5D (v
@@ -1679,66 +1679,200 @@
     
-return v + 1;%0A        %7D%0A      %7D%0A    %7D;%0A%0A    const result
+if (isNumber(v) && v === Math.round(v))%0A            return v;%0A          return CAST_ERROR;%0A        %7D%0A      %7D%0A    %7D;%0A%0A    assert.equal(validate(5, scheme, options), 5);%0A%0A    assert.throws(()
  =
+%3E
  val
@@ -1870,32 +1870,34 @@
 () =%3E validate(5
+.5
 , scheme, option
@@ -1902,36 +1902,157 @@
 ons)
-;%0A    assert.equal(result, 6
+,%0A      '%D1%81ouldn%5C't cast value to type INTEGER');%0A    assert.throws(() =%3E validate('5', scheme, options),%0A      '%D1%81ouldn%5C't cast value to type INTEGER'
 );%0A 

change test