0.0.1 • Published 6 years ago

@logankeenan/model v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

Model

Build Status

A way to build expressive models with validation through javascript classes and decorators. Inherits from Immutable-Model

Why?

Many modules exist which provide ways to validate models. However, many of them couple the consumer to an API or require a lot of configuration which is not easy to parse mentally. Validation Model solves these issues by using standard javascript classes and decorators to easily express validation rules.

Example

import Model, {required, maxLength, email} from '@logankeenan/model';

class Account extends Model {

    @required()
    @maxLength(255)
    get name() {
        return this._properties.name;
    }

    @required()
    @email()
    get email() {
        return this._properties.email;
    }
}

const newAccount = new Account({
    email: 'coffee'
});

newAccount.validate();
// returns { name: [ 'Required' ], email: [ 'Invalid email' ] }

More examples can be found in the examples directory.

Validation Functions

Simple customizable functions that determine the validity of a class property value. Validation functions make use of currying. The initial function arguments are reserved for any customization (customer error messages, max length of value, etc). The returned function is what will do the validation. Its arguments are the current value of the property and all other properties of the class. The return value is a string representing the failure or undefined representing success.

The following example validates that two properties of the class match:

export default (propertyToMatchOn) => (value, properties) => {
    if (value !== properties[propertyToMatchOn]) {
        return `Must match ${propertyToMatchOn} property`;
    }

    return undefined;
};

API

classInstance.validate()

Returns an object containing validation errors where the keys are class properties which validation errors exist and the values are arrays of strings presenting the error messages. Properties without validation errors will not exist in the returned validation object.

exposeFunctionAsDecorator

Makes a validation function available for usage as a decorator of a property.

Example:

import ImmutableModel, {exposeFunctionAsDecorator} from '@logankeenan/model';
import matchPropertyValidation from './match-property';

const matchProperty = exposeFunctionAsDecorator(matchPropertyValidation);

class Model extends ImmutableModel {
    get password() {
        return this._properties.password;
    }    
    
    @matchProperty('password')
    get confirmPassword() {
        return this._properties.confirmPassword;
    }
}

validation decorators

  • @boolean(optional customErrorMessage)
  • @creditCard(optional customErrorMessage)
  • @dateTime(optional customErrorMessage)
  • @dateTimeIsAfter(dependentProperty, optional customErrorMessage)
  • @dateTimeIsBefore(dependentProperty, optional customErrorMessage)
  • @email(optional customErrorMessage)
  • @float(optional customErrorMessage)
  • @integer(optional customErrorMessage)
  • @matchProperty(propertyToMatch, optional customErrorMessage)
  • @maxLength(maximumLengthOfString, optional customErrorMessage)
  • @minLength(minimumLengthOfString, optional customErrorMessage)
  • @regex(regularExpressionToMatch, optional customErrorMessage)
  • @required(optional customErrorMessage)
  • @requiredIfPropertyExists(dependentProperty, optional customErrorMessage)
  • @requiredIfPropertyDoesNotExist(dependentProperty, optional customErrorMessage)
  • @url(optional customErrorMessage)