Generate generic validators and models
$ npm install [--save-dev] @helpdotcom/help-gen
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.
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')
prop
NanoProp
Sets the item validator for an array
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
.
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Returns this
Returns an object that contains properties describing this property.
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?')
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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')
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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()
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Allow the email format like Evan Lucas <evanlucas@me.com>
.
Returns this
Returns an object that contains properties describing this property.
vals
<Array>
The accepted values
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')
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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()
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Allow the CIDR format like 192.168.100.0/22
.
Returns this
Returns an object that contains properties describing this property.
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)
n
<Number>
Sets the minimum value allowed
Returns this
n
<Number>
Sets the maximum value allowed. Must be > themin
Returns this
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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')
prop
<Array>
ofNanoProp
Sets the nested object validator props.
Note: do not include the path
of the parent in the child path.
Return this
Allows all properties returned in this object to passthrough the validator without being striped
Returns this
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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')
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
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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')
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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
n
<Number>
Sets the min length of the string. Must be >= 0
Returns this
n
<Number>
Sets the max length of the string. Must be > themin
.
Returns this
n
<Number>
Sets an exact length for the string. Cannot be used with min/max.
Returns this
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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')
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
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()
str
<String>
Sets thepath
property as an object-path (user.id
)
Returns this
val
<Boolean>
Is this property required?
Returns this
val
<Boolean>
Is this property unique?
Returns this
- The property is not required. Same as
required(false)
Returns this
str
<String>
Sets the description for the property
Returns this
val
Sets the example for this property
Returns this
val
<Boolean>
Allow the property to be null?
Allow the property to be null
Returns this
Returns an object that contains properties describing this property.
config
<Object>
ThetoJSON()
of aProp
Returns a Prop
based on config.type
.
config
[<Config>
] Adoc.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.
arg
{Any}
Checks if arg
is an instance of a NanoProp
Returns a <Boolean>
.
This will generate one-off generic validators.
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')
]
}
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')
}
conf
<Object>
A single model config
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)
}
$ npm test
Evan Lucas
MIT (See LICENSE
for more info)