Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Simple model validation framework for Spine. Allowing you to mix strong validation rules with fluid english context.

Notifications You must be signed in to change notification settings

sprucemedia/spine.validate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple model validation framework for Spine. Allowing you to mix strong validation rules with fluid english context.

Install

Download spine.validation.js and include it in your html.

<script type="text/javascript" src="spine.validate.js"></script>

Usage

You must first start with a spine model

    var model = Spine.Model.setup("Person", ["first","last","age","birth","address1","city","state","zip"]);

Then include the validation object

    model.include(Spine.Validate);

Then setup some super awesome rules to go along with that

    model.include({
        rules: function(check) { return [
            RuleFor("first")
                .WhenNot().OnCreate()
                .ItShouldBe()
                .Required(),

            RuleFor("first")
                .It()
                .Must(function(field,record) {
                    return field === record.last;
                })
                .When(function(record) {
                    return record.last === "Palmer";
                })
                .Message("first and last names must match"),

            RuleFor("last")
                .ItIs()
                .Required()
                .And().Also()
                .Matches(/[A-Z]+/i),

            RuleFor("age")
                .ItShouldBe()
                .Between(18,25),

            RuleFor("birth")
                .It()
                .IsInPast(),

            RuleFor("state")
                .ItIs()
                .Required()
                .When(function(record) {
                    return record.zip !== undefined && record.zip.length > 0
                })
                .And().MaxLength(2),

            RuleFor("zip")
                .WhenNot().OnCreate()
                .It().IsNumeric()
                .Also().ItIs().Length(5),

            RuleFor("email")
                .ItIs()
                .EmailAddress()
        ]}
    });

After that whenever spine would normally call your custom "validate" method it will run through all the rules and send out error events just like you are used too. If you aren't familiar with this process see the spine documentation.

Contributors

  • Nathan Palmer (author)
  • Aaron Hansen
  • Jason Leveille

About

Simple model validation framework for Spine. Allowing you to mix strong validation rules with fluid english context.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.1%
  • Ruby 0.9%