Framework

- About
- Guides
- Community
- API
- Code
- Download

# Kanso API Documentation

Index | View on single page

## types

Document types can be used to ease the validation of updates and check permissions when creating, editing or deleting documents.

### Type(name, options)

Creates a new Type object

#### Options

- fields Object - Field objects to use as the Types's schema
- permissions Object - a permissions check function or an object containing separate functions to run on add, remove and update operations.
- display_name Function|String|Array - name to be used when displaying the document in the admin tool. A string or array will become the property to display. A function should take the document as a object and return the display name.

#### Parameters

options | Object |

### Type.validate(doc, rawDoc)

Run field validators against document and check for missing required fields or extra fields when the Types's allow_extra_fields property is set to false.

#### Parameters

doc | Object |
rawDoc | Object |

Returns: Array

### Type.authorize(nDoc, oDoc, user)

Run field permissions checks against userCtx and document.

#### Parameters

nDoc | Object | new document
oDoc | Object | old document
userCtx | Object | user context object

Returns: Array

### Type.authorizeTypeLevel(nDoc, oDoc, user)

Runs top type-level permissions checks only.

#### Parameters

nDoc | Object | new document
oDoc | Object | old document
userCtx | Object | user context object

Returns: Array

### Type.create(userCtx, callback)

Create's a new object for this Type. Pre-filling any default values and providing a new _id value. This is a convenient function to use when creating a type to embed within another.

#### Parameters

userCtx | Object |
callback | Function |

### validate_doc_update(types, newDoc, oldDoc, userCtx)

Calls validation and permissions functions relevant to a document update. This should be called from your app's exported validate_doc_update function if you wish to use kanso Types in you project.

Throws on error.

#### Parameters

types | Object |
newDoc | Object |
oldDoc | Object |
userCtx | Object |