'use strict'

const test = require('tape')

const {validateField} = require('./validate-record-rypes')

test.only('validateField(value, key)', (t) => {
  t.throws(
    () => validateField('not an object', 'aField'),
    /The definition of "aField" must be an object/
  )

  t.throws(
    () => validateField({ type: Number }, 'id'),
    /Cannot define primary key "id"\. Probably you need "\$id"/
  )

  t.throws(
    () => validateField({}, 'withoutTypeAndLink'),
    /The definition of "withoutTypeAndLink" must contain either the "type" or "link" property/
  )

  t.throws(
    () => validateField({ type: Number, link: 'some' }, 'typeAndLink'),
    /Cannot define both "type" and "link" on "typeAndLink"/
  )

  t.throws(
    () => validateField({type: () => {}}, 'someWrongType'),
    /The "type" on "someWrongType" is invalid/
  )

  t.throws(
    () => validateField({type: {}}, 'someWrongType'),
    /The "type" on "someWrongType" is invalid/
  )

  t.throws(
    () => validateField({type: Buffer}, 'someWrongType'),
    /The "type" on "someWrongType" is invalid/
  )

  const nativeTypes = [ String, Number, Boolean, Date ]
  nativeTypes.forEach((type) => {
    t.doesNotThrow(() => validateField({type: type}, 'rightType'))
  })

  t.throws(
    () => validateField({link: {}}, 'wrongLink'),
    /The "link" on "wrongLink" must be a string/
  )

  t.throws(
    () => validateField({link: 'some', isArray: {}}, 'isArrayNotBoolean'),
    /The key "isArray" on "isArrayNotBoolean" must be a boolean/
  )

  t.end()
})

@@ -44,16 +44,26 @@
 %0Aconst %7B
+validate, 
 validate
@@ -115,13 +115,150 @@
 test
-.only
+('validate(fields)', (t) =%3E %7B%0A  t.throws(%0A    () =%3E validate('not an object'),%0A    /Type definition must be an object/%0A  )%0A%0A  t.end()%0A%7D)%0A%0Atest
 ('va

add validate(fields) test