Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/izumisy/validatable-record
Immutable.js Record powered with validate.js
https://github.com/izumisy/validatable-record
immutablejs record validate-js validation
Last synced: 6 days ago
JSON representation
Immutable.js Record powered with validate.js
- Host: GitHub
- URL: https://github.com/izumisy/validatable-record
- Owner: IzumiSy
- License: mit
- Created: 2017-08-28T10:59:24.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-11T01:53:30.000Z (5 months ago)
- Last Synced: 2024-09-15T03:31:51.750Z (about 2 months ago)
- Topics: immutablejs, record, validate-js, validation
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/validatable-record
- Size: 493 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# validatable-record
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/IzumiSy/validatable-record/tree/master.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/IzumiSy/validatable-record/tree/master)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![npm version](https://badge.fury.io/js/validatable-record.svg)](https://badge.fury.io/js/validatable-record)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
> Immutable.js Record powered with validate.js## Table of Contents
- [Install](README.md#Install)
- [Usage](README.md#Usage)
- [Contribute](README.md#Contribute)
- [Test](README.md#Test)
- [License](README.md#License)## Install
```bash
$ npm install --save validatable-record
```## Usage
ValidatableRecord returns `Record` in Immutable.js for extending your own class. Usage is almost the same as `Record` in Immutable.js, but it has the power of `validate.js`. With ValidatableRecord, you can define models with built-in validation logic.
```js
const ManRecord = ValidatableRecord({
name: null,
age: null
}, {
name: {
presence: true
},
age: {
presence: {
message: "is invalid"
}
}
});class Man extends ManRecord {
...
}const man = new Man({
name: "Justine";
age: 25
});man.validate() // == true
// Of course you can use `Immutable.Record` methods
man.size // 2
man.get('name') // "Justine"
man.get('age') // 25const agelessMan = new Man({
name: "Michael"
});agelessMan.validate() // == false
agelessMan.getErrors() // == [ "Age is invalid" ]
// You can set your own error to model
agelessMan.setError("Unknown error")agelessMan.getErrors() // = [ "Unknown error" ]
```## Test
```bash
$ npm test
```## Contribute
PRs accepted.## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).