Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Based Validations #8

Open
anywhichway opened this issue Dec 10, 2016 · 1 comment
Open

Schema Based Validations #8

anywhichway opened this issue Dec 10, 2016 · 1 comment
Assignees

Comments

@anywhichway
Copy link
Owner

Schema based validations are being considered. This feature would support the optional use of schema to support object validation, default values, and index specification.

This topic is open for questions, comments, and design thoughts. Give it a thumbs-up if it is important to you.

@mingard
Copy link

mingard commented Jan 19, 2017

I have been working on something for this. It consists of:

Schema config JSON

User schema

{
  "fields": {
    "name": {
      "type": "String"
    },
    "age": {
      "type": "Number"
    },
    "group": {
      "type": "Group"
    }
  }
}

Group schema

{
  "fields": {
    "title": {
      "type": "String"
    },
    "createdAt": {
      "type": "Date"
    }
  }
}

Schema Builder

'use strict'

const fs = require('fs')
const path = require('path')
const _ = require('underscore')
const _s = require('underscore.string')

var classes = {
  String: class extends Field {},
  Object: class extends Field {},
  Array: class extends Field {},
  Number: class extends Field {},
  Date: class extends Field {}
}

const Schemas = function () {
  // Get all schema files
  let schemas = fs.readdirSync(path.join(__dirname, 'schemas'))
  // Iterate schema appending classes
  this.iterateSchemas(schemas)
}

Schemas.prototype.iterateSchemas = function (schemas) {

  Object.assign(classes, ..._.map(schemas, (schema) => {
    let json = this.getSchema(schema)
    return {
      [json.classname]: class {
        constructor (data) {
          if (!_.isUndefined(data)) {
            _.each(json.fields, (field, key) => {
              if (data[key] && classes[field.type]) {
                this[key] = new classes[field.type](data[key]) 
              }
            })
          }
        }
      }
    }
  }))
}

Schemas.prototype.getSchemas = function (schema) {
  let content = fs.readFileSync(path.join(__dirname, 'schemas', schema),'utf8')
  let body = JSON.parse(content)
  return {
    classname: _s.capitalize(path.parse(schema).name),
    fields: body.fields
  }
}

module.exports = function () {
  return new Schemas()
}

Allowing nested validation too, simply with:

let schemas = new Schemas()

new schemas.Users({
  "name": "Joe",
  "age" : 31,
  "group": {
    "title": "My Group",
    "createdAt": 1484833503
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants