Skip to content

Formatting Support ? #5

@1fabiopereira

Description

@1fabiopereira

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]'
    }]
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions