<commit_before>/* global describe, it */

'use strict'

require('should')
var osom = require('..')

describe('schema defintion', function () {
  describe('simple rule', function () {
    it('empty value', function () {
      [{age: Number}, {age: { type: Number }}].forEach(function (rule) {
        osom(rule)().should.be.eql({})
      })
    })

    describe('providing value', function () {
      it('Number', function () {
        [
          {age: Number},
          {age: { type: Number }}
        ].forEach(function (rule) {
          osom(rule)({age: '23'}).should.be.eql({age: 23})
        })
      })

      it('Array', function () {
        [
          {age: Array},
          {age: { type: Array }}
        ].forEach(function (rule) {
          osom(rule)({age: ['23']}).should.be.eql({age: ['23']})
        })
      })
    })
  })

  it('empty values', function () {
    [Number, String, Function, Boolean].forEach(function (type) {
      var schema = {
        age: {
          type: type
        }
      }

      var validator = osom(schema)
      ;[null, {}].forEach(function (data) {
        validator(data).should.be.eql({})
      })
    })
  })

  describe('support default values', function () {
    it('based in a value', function () {
      var schema = {
        age: {
          type: Number, default: 23
        }
      }

      var validator = osom(schema)
      validator().should.be.eql({age: 23})
    })

    it('based in a fn', function () {
      var schema = {
        age: {
          type: Number, default: function () { return 23 }
        }
      }

      var validator = osom(schema)
      validator().should.be.eql({age: 23})
    })

    it('based in a fn with precedence given to provided values', function () {
      var rand = () => String(Math.random())
      var schema = {
        age: {
          type: String, default: rand
        }
      }

      var validator = osom(schema, { casting: false })
      validator({ age: '23' }).should.be.eql({ age: '23' })
    })
  })

  it('support transforms', function () {
    function trim (str) {
      return str.trim()
    }

    var schema = {
      age: {
        type: String,
        transform: [trim]
      }
    }

    var validator = osom(schema)
    validator({age: '  23  '}).should.be.eql({age: '23'})
  })

  describe('support required values', function () {
    it('throw an error under no values', function () {
      var schema = {
        age: {
          type: String,
          required: true
        }
      }

      var validator = osom(schema)
      var errMessage = "Expected {string} for 'age'."
      ;(function () { validator() }).should.throw(errMessage)
    })

    it('custom error message', function () {
      var schema = {
        age: {
          type: String,
          required: 'your message here'
        }
      }

      var validator = osom(schema)
      var errMessage = 'your message here'
      ;(function () { validator() }).should.throw(errMessage)
    })
  })

  describe('support casting', function () {
    it('enable (by default)', function () {
      var schema = { age: Array }
      var validator = osom(schema)
      validator({age: '23'}).should.be.eql({age: ['23']})
    })

    it('disable explicit', function () {
      var schema = {
        age: {
          type: String,
          casting: false
        }
      }

      var errMessage = "Expected {string} for 'age'."
      var validator = osom(schema)

      ;[{age: 23}].forEach(function (obj) {
        ;(function () { validator(obj) }).should.throw(errMessage)
      })
    })

    it('disable explicit works with nill values', function () {
      [Number, String, Function, Boolean].forEach(function (type) {
        var schema = {
          age: {
            type: type,
            casting: false
          }
        }

        var validator = osom(schema)
        ;[null, {}, {age: null}].forEach(function (data) {
          validator(data).should.be.eql({})
        })
      })<commit_msg>Refactor specific test for casting disabled<commit_after>/* global describe, it */

'use strict'

require('should')
var osom = require('..')

describe('schema defintion', function () {
  describe('simple rule', function () {
    it('empty value', function () {
      [{age: Number}, {age: { type: Number }}].forEach(function (rule) {
        osom(rule)().should.be.eql({})
      })
    })

    describe('providing value', function () {
      it('Number', function () {
        [
          {age: Number},
          {age: { type: Number }}
        ].forEach(function (rule) {
          osom(rule)({age: '23'}).should.be.eql({age: 23})
        })
      })

      it('Array', function () {
        [
          {age: Array},
          {age: { type: Array }}
        ].forEach(function (rule) {
          osom(rule)({age: ['23']}).should.be.eql({age: ['23']})
        })
      })
    })
  })

  it('empty values', function () {
    [Number, String, Function, Boolean].forEach(function (type) {
      var schema = {
        age: {
          type: type
        }
      }

      var validator = osom(schema)
      ;[null, {}].forEach(function (data) {
        validator(data).should.be.eql({})
      })
    })
  })

  describe('support default values', function () {
    it('based in a value', function () {
      var schema = {
        age: {
          type: Number, default: 23
        }
      }

      var validator = osom(schema)
      validator().should.be.eql({age: 23})
    })

    it('based in a fn', function () {
      var schema = {
        age: {
          type: Number, default: function () { return 23 }
        }
      }

      var validator = osom(schema)
      validator().should.be.eql({age: 23})
    })
  })

  it('support transforms', function () {
    function trim (str) {
      return str.trim()
    }

    var schema = {
      age: {
        type: String,
        transform: [trim]
      }
    }

    var validator = osom(schema)
    validator({age: '  23  '}).should.be.eql({age: '23'})
  })

  describe('support required values', function () {
    it('throw an error under no values', function () {
      var schema = {
        age: {
          type: String,
          required: true
        }
      }

      var validator = osom(schema)
      var errMessage = "Expected {string} for 'age'."
      ;(function () { validator() }).should.throw(errMessage)
    })

    it('custom error message', function () {
      var schema = {
        age: {
          type: String,
          required: 'your message here'
        }
      }

      var validator = osom(schema)
      var errMessage = 'your message here'
      ;(function () { validator() }).should.throw(errMessage)
    })
  })

  describe('support casting by default', function () {
    it('enable (by default)', function () {
      var schema = { age: Array }
      var validator = osom(schema)
      validator({age: '23'}).should.be.eql({age: ['23']})
    })
  })

  describe('casting disabled explicitly', function () {
    it('throw an error under different type provided', function () {
      var schema = {
        age: {
          type: String
        }
      }

      var errMessage = "Expected {string} for 'age'."
      var validator = osom(schema, {casting: false})

      ;[{age: 23}].forEach(function (obj) {
        ;(function () { validator(obj) }).should.throw(errMessage)
      })
    })

    it('works with nill values of the same type', function () {
      [Number, String, Function, Boolean].forEach(function (type) {
        var schema = {
          age: {
            type: type
          }
        }

        var validator = osom(schema, {casting: false})
        ;[null, {}, {age: null}].forEach(function (data) {
          validator(data).should.be.eql({})
        })
      })
    })

    it('works with default value (not necessary of the same type defined)', function () {
      var schema = {
        age: {
          type: String,
          default: 23
        }
      }

      var validator = osom(schema, { casting: false })
      validator({}).should.be.eql({ age: 23 })
    })

    it('works with a value provided of the same type', function () {
      var schema = {
        age: {
          type: String,
          default: '24'
        }
      }

      var validator = osom(schema, { casting: false })
      validator({ age: '23' }).should.be.eql({ age: '23' })
{'fraction_non_alphanumeric': 0.1616838077015068, 'fraction_numerical': 0.007892848600813202, 'mean_word_length': 1.8699382292381606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7409956', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2505, 'n_words': 623}