2.0.3 • Published 6 years ago

model-base v2.0.3

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

Build Status Coverage Status NPM Version NPM Downloads Dependency Status DevDependency Status Greenkeeper badge

model-base

Classes to extend and help create useful data models with strict validation

Installation

yarn add model-base

Usage

# /api/models/analytics.js

'use strict';

const Model = require('model-base').Model;
const ModelCollection = require('model-base').ModelCollection;

const AnalyticsAttributes = {
	id: {
		type: 'integer',
		required: true,
		description: 'analytics datum id'
	},
	metric: {
		type: 'string',
		required: true,
		description:'type of analytics datum'
	},
	value: {
		type:'float',
		required: true,
		description:'value of metric'
	}
};

class AnalyticsModel extends Model {
	constructor(values) {
		super(AnalyticsAttributes, values);
	}
}

class AnalyticsModelCollection extends ModelCollection {
	constructor(values, coerce) {
		super(AnalyticsModel, values, coerce);
	}
}

module.exports.AnalyticsModel = AnalyticsModel;
module.exports.AnalyticsAttributes = AnalyticsAttributes;
module.exports.AnalyticsCollection = AnalyticsModelCollection;
# /api/controllers/analytics.js

'use strict';

let ModelFile = require(`/api/models/analytics`);
let ModelCollection = ModelFile.AnalyticsCollection;

let collection = new ModelCollection(result.rows, true);
return collection.validate().getValues(true);

List Of Validation Types

'array',
'buffer',
'string',
'boolean',
'ip',
'url',
'slug',
'uuid',
'fqdn',
'json',
'email',
'alpha',
'base64',
'hex',
'alpha_numeric',
'phone',
'function',
'date',
'time',
'datetime',
'daterange',
'timestamp',
'integer',
'float',
'number',
'object'

List Of Attribute Options

  • type (required) - type of validation function to run on value

  • required (default: false) - states whether the value is required or not

  • default_value - any value or function that returns a value that will be set if no value is present

  • custom - array of custom validation functions that allow you to do further validations on a value - return a string value with the error if there is an error or nothing if no error

  • transform - function to transform input into any output - must return a value or the value will be set to undefined

  • choices - array of possible values for model attribute - if the value is not one of the choices an error will be thrown

  • alias - key for other model attribute that keeps the values the same - it passes the value from the other attribute into this one

Contributing

Installation

  • git clone <repository-url>
  • cd model-base
  • yarn install

Linting

  • yarn lint

Running tests

  • yarn test – Runs the test suite on the current Node Version

License

This project is licensed under the MIT License.

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

1.2.5

6 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.1

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago