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

Formatting Support ? #5

Open
1fabiopereira opened this issue Jan 17, 2017 · 3 comments
Open

Formatting Support ? #5

1fabiopereira opened this issue Jan 17, 2017 · 3 comments

Comments

@1fabiopereira
Copy link

A feature that I deem useful and can be added in future versions if following the purpose of this library is the possibility of passing a formatting function to the field, for example:

sample.model

/*
 * An example data model
 */

Date created
Number id

String name {
  required true // this is a comment
  gt 2 "Must be greater than 2 characters"
  lte 256 "Must be less than or equal to 256 characters"
  formatter  (str)=> str.toUpperCase()
}

String bio "A bio must be a string" {
  lte 140 "A bio must fit into a tweet"
}

Boolean accountType
}

index.js

const Models = require('node-dml')

let model = Models.compile(fs.readFileSync('sample.model', 'utf8'))

let result = model({
  id: 1337,
  created: new Date(),
  name: 'Glen Danzig',
  accountType: 'awesome'
})

Output

{
  data: {
    id: 1337,
    created: '2016-10-02T13:56:44.931Z',
    name: 'GLEN DANZIG', // Formatted in uppercase
    accountType: 'awesome'
  },
  length: 1,
  rules: {
    accountType: [{
      validator: 'type',
      message: 'Expected type [Boolean] but got type [String]'
    }]
  }
}
@heapwolf
Copy link
Member

I like that idea, a pull request would certainly be welcome. It might be tough to find the end of the javascript or parse properly. Another option though, is adding a plugin system, which could be pretty cool. then we could npm install various advanced validators. i had something like this in the back of my mind while i was originally writing this version. this code might help make a plugin system reasonable to implement.

@1fabiopereira
Copy link
Author

Perfect I'll study your code and see how it works in the background because to be honest I got lost sometimes in it and as much as possible I can check which better option the plugin or add directly to the lib

@1fabiopereira
Copy link
Author

How did you imagine this system of plugins ?

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

No branches or pull requests

2 participants