npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@helpdotcom/help-gen

v11.0.2

Published

Generate validators for help.com services

Downloads

131

Readme

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('[email protected]')
  .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 <[email protected]>.

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)