11.0.2 • Published 5 years ago

@helpdotcom/help-gen v11.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

help-gen

Generate generic validators and models

Install

$ npm install [--save-dev] @helpdotcom/help-gen

Documentation

Prop

A prop represents a base configuration object for a single property that is intended to be used by a validator or a model.

All properties are required unless optional() or required(false) is called.

Prop.array()

Returns a Prop that represents an <Array>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

// Support validating primitives in arrays
Prop
  .array()
  .path('department_names')
  .required(false)
  .example([
    'General'
  ])
  .props(Prop.string())
  .description('Contains the department names')
props(prop)
  • prop NanoProp Sets the item validator for an array
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false).

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.boolean()

Returns a Prop that represents a <Boolean>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .boolean()
  .path('accepts_offline_messages')
  .optional()
  .example(false)
  .description('Does the org accept offline messages?')
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.date()

Returns a Prop that represents a <Date>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .date()
  .path('modified_at')
  .optional()
  .example(new Date().toISOString())
  .description('The ISO String of the modified date')
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.email()

Returns a Prop that represents a email.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .email()
  .path('email')
  .example('evan@biscuits.io')
  .description('The user\'s email address')
  .allowName()
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

allowName()

Allow the email format like Evan Lucas <evanlucas@me.com>.

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.enum(vals)

Returns a Prop that represents an enum.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .enum(['chat', 'helpdesk', 'voice'])
  .path('channel')
  .required(false)
  .example('chat')
  .description('The department\'s channel')

// Alternative API

Prop
  .enum()
  .values(['chat', 'helpdesk', 'voice'])
  .path('channel')
  .required(false)
  .example('chat')
  .description('The department\'s channel')
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.ip()

Returns a Prop that represents a ip.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .ip()
  .path('ip')
  .example('172.0.0.1')
  .description('An Ip')
  .allowCIDR()
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

allowCIDR()

Allow the CIDR format like 192.168.100.0/22.

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.number()

Returns a Prop that represents a <Number>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .number()
  .path('chat_slots')
  .optional()
  .example(3)
  .description('The number of chat slots the agent has available')
  .min(1)
  .max(10)
min(n)
  • n <Number> Sets the minimum value allowed

Returns this

max(n)
  • n <Number> Sets the maximum value allowed. Must be > the min

Returns this

path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.object()

Returns a Prop that represents an object.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .object()
  .path('payload')
  .required(false)
  .example({
    foo: 'bar'
  })
  .props([
    Prop.uuid().path('id')
  , Prop.string().path('name').min(1)
  , Prop.string().path('url').optional().allowNull()
  ])
  .description('Holds the entire payload')
props(prop)
  • prop <Array> of NanoProp Sets the nested object validator props.

Note: do not include the path of the parent in the child path.

Return this

passthrough()

Allows all properties returned in this object to passthrough the validator without being striped

Returns this

path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.ref(name)

Returns a Prop that represents a reference to another named model/validator.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .ref('Visitor')
  .path('visitor')
  .description('This is the visitor of the object')

// Alternatively, to mark a reference as an array of references
Prop
  .ref('Visitor')
  .path('visitors')
  .multi()
  .description('List of visitors associated with this room')
multi()

Sets the multi property of the validator or model that we are targeting. This represents that the reference is for an <Array> of objects that are represented by the named ref.

Returns this

path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.regex(value)

Returns a Prop that represents a <RegExp>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .regex(/biscuits/)
  .path('some.random.thing')
  .optional()
  .example('biscuits')
  .description('Whatever')

// Alternative API

Prop
  .regex()
  .value(/biscuits/)
  .path('some.random.thing')
  .optional()
  .example('biscuits')
  .description('Whatever')
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.string()

Returns a Prop that represents a <String>.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .string()
  .path('name')
  .required(false)
  .example('Evan')
  .description('Tim will have the reuben')
  .min(1)  // require length of at least 1
  .max(10) // allow length of up to 10

Prop
  .string()
  .path('my_key')
  .example('abc1234567')
  .description('Our keys are always 10 characters')
  .len(10) // .length must equal this
min(n)
  • n <Number> Sets the min length of the string. Must be >= 0

Returns this

max(n)
  • n <Number> Sets the max length of the string. Must be > the min.

Returns this

len(n)
  • n <Number> Sets an exact length for the string. Cannot be used with min/max.

Returns this

path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.uuid()

Returns a Prop that represents a uuid.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .uuid()
  .path('id')
  .optional()
  .example('C04DB833-D21C-4C9C-BD5C-16DE42B83207')
  .description('The model id')
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.url()

Returns a Prop that represents a url.

Example:

'use strict'

const {Prop} = require('@helpdotcom/help-gen')

Prop
  .url()
  .path('url')
  .example('http://help.com')
  .description('A URL')
  .allowName()
path(str)
  • str <String> Sets the path property as an object-path (user.id)

Returns this

required(val)

Returns this

unique(val)

Returns this

optional()
  • The property is not required. Same as required(false)

Returns this

description(str)
  • str <String> Sets the description for the property

Returns this

example(val)
  • val Sets the example for this property

Returns this

allowNull(val)

Allow the property to be null

Returns this

toJSON()

Returns an object that contains properties describing this property.


Prop.fromConfig(config)

Returns a Prop based on config.type.


Prop.fromConfigList(config)

  • config <Config> A doc.json validator config object

Returns an array of Prop objects based on the type.

Note: This method is intended strictly for converting doc.json validator trees back into Prop trees.


Prop.isProp(arg)

  • arg {Any}

Checks if arg is an instance of a NanoProp

Returns a <Boolean>.


Validator

This will generate one-off generic validators.


ModelManager

This will generate a set of models that are able to reference each other.

Every model must have a model config. A model config will look something like the following:

{ name: 'VisitorJoin'
, type: 'visitor_join'
, includeType: true
, props: [
    Prop.date().path('created_at')
  , Prop.ref('Visitor').path('visitor')
  ]
}

ModelManager(opts)

Example:

'use strict'

const Manager = require('@helpdotcom/nano-model')
const manager = new Manager({
  configs: [
    { name: 'Visitor'
    , type: 'visitor'
    , props: [
        { name: 'Visitor'
        , type: 'visitor'
        , props: [
            Prop.uuid().path('id')
          , Prop.string().path('name')
          ]
        }
      , { name: 'VisitorJoin'
        , type: 'visitor_join'
        , includeType: true
        , props: [
            Prop.date().path('created_at')
          , Prop.ref('Visitor').path('visitor').optional()
          ]
        }
      ]
    }
  ]
})

const out = manager.generate()

// To write all of the models, one can do something like this:
const fs = require('fs')
const path = require('path')
const MODELS_DIR = path.join(process.cwd(), 'models')

for (const item of out.values()) {
  const fp = path.join(MODELS_DIR, item.filename)
  fs.writeFileSync(fp, item.code, 'utf8')
}
define(conf)
generate()

Returns a <Map> where the key is the model name and the value is an object like:

{ code: ''                // Will be the actual code
, filename: 'visitor.js'  // Will be the filename (does not include the path)
}

Test

$ npm test

Author

Evan Lucas

License

MIT (See LICENSE for more info)

11.0.2

5 years ago

11.0.1

5 years ago

11.0.0

6 years ago

10.3.0

6 years ago

10.2.0

6 years ago

10.1.0

6 years ago

10.0.1

6 years ago

10.0.0

6 years ago

9.3.0

6 years ago

9.2.0

7 years ago

9.1.0

7 years ago

9.0.1

7 years ago

9.0.0

7 years ago

8.4.5

7 years ago

8.4.4

7 years ago

8.4.3

7 years ago

8.4.2

7 years ago

8.4.1

7 years ago

8.4.0

7 years ago

8.3.0

7 years ago

8.2.4

7 years ago

8.2.3

7 years ago

8.2.2

7 years ago

8.2.1

7 years ago

8.2.0

7 years ago

8.1.2

7 years ago

8.1.1

7 years ago

8.1.0

7 years ago

8.0.8

7 years ago

8.0.7

7 years ago

8.0.6

7 years ago

8.0.5

7 years ago

8.0.4

7 years ago

8.0.3

7 years ago

8.0.2

7 years ago

8.0.1

7 years ago

8.0.0

7 years ago

7.1.0

7 years ago

7.0.0

7 years ago

6.0.0

8 years ago

5.0.4

8 years ago

5.0.3

8 years ago

5.0.2

8 years ago

5.0.1

8 years ago

5.0.0

8 years ago

4.4.1

8 years ago

4.4.0

8 years ago

4.3.0

8 years ago

4.2.0

8 years ago

4.1.0

8 years ago

4.0.5

8 years ago

4.0.4

8 years ago

3.1.1

8 years ago

4.0.3

8 years ago

4.0.2

8 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.1.0

8 years ago

3.0.0

8 years ago

2.1.5

8 years ago

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago