const R = require('ramda')
const {Maybe} = require('folktale/data')

const asArray = R.concat([])
const rejectNils = R.reject(R.isNil)

class SchemaNode {
  constructor ({env, schemaStack}) {
    // TODO: I don't think this should ever be empty. Verify.
    this.schema = R.head(schemaStack)

    let toTypeValidators = R.compose(rejectNils, R.map((t) => env.types[t]), asArray)
    let toFormatValidators = R.compose(rejectNils, R.map((f) => env.formats[f]), asArray)

    let typeValidators, formatValidators

    typeValidators = this.prop('type').map(toTypeValidators).getOrElse([])
    formatValidators = this.prop('format').map(toFormatValidators).getOrElse([])

    this._validators = typeValidators.concat(formatValidators)
  }

  prop (name) {
    return Maybe.fromNullable(R.prop(name, this.schema))
  }

  get validators () {
    return this._validators
  }

  get children () {

  }
}

SchemaNode.from = (n) => new SchemaNode(n)

module.exports = SchemaNode

@@ -32,16 +32,28 @@
 t %7BMaybe
+, Validation
 %7D = requ
@@ -74,16 +74,55 @@
 data')%0A%0A
+const %7BSuccess, Failure%7D = Validation%0A%0A
 const as
@@ -180,16 +180,142 @@
 isNil)%0A%0A
+// TODO: Should live elsewhwere%0Aconst required = (p) =%3E (o) =%3E o.hasOwnProperty(p) ? Success(o) : Failure(%5B%7B'required': p%7D%5D)%0A%0A
 class Sc
@@ -638,16 +638,105 @@
 asArray)
+%0A    let toRequiredValidators = R.compose(rejectNils, R.map((p) =%3E required(p)), asArray)
 %0A%0A    le
@@ -769,16 +769,36 @@
 lidators
+, requiredValidators
 %0A%0A    ty
@@ -946,16 +946,103 @@
 Else(%5B%5D)
+%0A    requiredValidators = this.prop('required').map(toRequiredValidators).getOrElse(%5B%5D)
 %0A%0A    th
@@ -1096,16 +1096,36 @@
 lidators
+, requiredValidators
 )%0A  %7D%0A%0A 

Implement `required` validator