<commit_before># Backbone Declarative Validator


This is a light weight add-on to the Backbone.js framework that provides declarative validation capabilities.

In keeping with the spirit of Backbone.js, this add-on is un-opinionated in its approach to validation.

In an attempt to operate within the confines of a quasi-MVC architectural pattern, this add-on only provides model level validation and does not offer an API that can be used to manipulate views. It does however, expose an `errors` object that can be used as necessary.

## Installation

This add-on requires `Underscore.js` and `Backbone.js` to be installed as prerequisites. 

On synchronous pages:

```
	<script type='text/javascript' src="backbone.declarative.validator.js">/<script>

```

When using a service locator such as RequireJS the add-on needs to be installed as a shim.

```
require.config({

	paths: {
		"Backbone": "lib/backbone-min.js",
		"Backbone.Declarative.validator": "lib/backbone.declarative.validator.min.js""
	},
	
	shim: {
		"Backbone": {
			deps: ["Underscore", "jQuery"],
			exports: "Backbone"
		},
		"Backbone.Declarative.Validator": {
			deps: ["Backbone"],
			exports: "Backbone"
		},
	}
});

```

## Usage

The attributes to be validated are declared as a hash in the model's constructor.

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true,
			type: "text",
			msg: "Name can consist only of alphabets"
		},
		email: {
			presence: true,
			type: "email"
		}
	}

});

```
The following are the list of validations currently supported.

### Presence

This checks whether the attribute has a defined value or not. Empty and whitespace strings, `null` and `undefined` values are considered invalid. 

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true
		}
	}

});

```

Setting `presence: false` will validate *only* empty (including whitespace) strings, null or undefined values; **do not** set `presence` for optional attributes.


### Type

Validating specific types of values can be performed by setting this value for an attribute in the `validates` hash. Type validations also validate empty strings (to prevent this use `presence`). Currently supported type validations are listed below:

* text
* number
* email
* size

#### text

Validates a string containing only alphabets. 

```
var User = Backbone.Model.extend({
	
	validates: {
		first_name: {
			presence: true,
			type: 'text'
		}
	}	
	
});

```

#### number

Validates (quoted and un-quoted) values containing only numbers (integers as well as floats). 

```
var Account = Backbone.Model.extend({
	
	validates: {
		balance: {
			presence: true,
			type: "number"
		}
	}
	
});

```

#### email

Validates email addresses

```
var User = Backbone.Model.extend({

	validates: {
		email: {
			type: "email"
		}
	}

});

```

#### size

This validates strings for length and numbers for magnitude. Quoted numbers are validated for magnitude and not length.

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true,
			type: "text",
			size: "> 40" // Validates length
		}
		age: {
			presence: true,
			type: "number",
			size: "> 21" // Validates magnitude
		}
	}
	
});

```

## Overriding Type Validation

It is possible to provide a custom pattern for an existing type with the ```using``` property.

```
var User = Backbone.Model.extend({
	
	validates: {	
		first_name: {
			type: "text",
			using: /^(\s*([a-zA-Z0-9]+)\s*)$/gi // Now validates alpha-numeric strings
		}
	}<commit_msg>Edited README - added documentation for size conditions<commit_after># Backbone Declarative Validator


This is a light weight add-on to the Backbone.js framework that provides declarative validation capabilities.

In keeping with the spirit of Backbone.js, this add-on is un-opinionated in its approach to validation.

In an attempt to operate within the confines of a quasi-MVC architectural pattern, this add-on only provides model level validation and does not offer an API that can be used to manipulate views. It does however, expose an `errors` object that can be used as necessary.

## Installation

This add-on requires `Underscore.js` and `Backbone.js` to be installed as prerequisites. 

On synchronous pages:

```
	<script type='text/javascript' src="backbone.declarative.validator.js">/<script>

```

When using a service locator such as RequireJS the add-on needs to be installed as a shim.

```
require.config({

	paths: {
		"Backbone": "lib/backbone-min.js",
		"Backbone.Declarative.validator": "lib/backbone.declarative.validator.min.js""
	},
	
	shim: {
		"Backbone": {
			deps: ["Underscore", "jQuery"],
			exports: "Backbone"
		},
		"Backbone.Declarative.Validator": {
			deps: ["Backbone"],
			exports: "Backbone"
		},
	}
});

```

## Usage

The attributes to be validated are declared as a hash in the model's constructor.

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true,
			type: "text",
			msg: "Name can consist only of alphabets"
		},
		email: {
			presence: true,
			type: "email"
		}
	}

});

```
The following are the list of validations currently supported.

### Presence

This checks whether the attribute has a defined value or not. Empty and whitespace strings, `null` and `undefined` values are considered invalid. 

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true
		}
	}

});

```

Setting `presence: false` will validate *only* empty (including whitespace) strings, null or undefined values; **do not** set `presence` for optional attributes.


### Type

Validating specific types of values can be performed by setting this value for an attribute in the `validates` hash. Type validations also validate empty strings (to prevent this use `presence`). Currently supported type validations are listed below:

* text
* number
* email
* size

#### text

Validates a string containing only alphabets. 

```
var User = Backbone.Model.extend({
	
	validates: {
		first_name: {
			presence: true,
			type: 'text'
		}
	}	
	
});

```

#### number

Validates (quoted and un-quoted) values containing only numbers (integers as well as floats). 

```
var Account = Backbone.Model.extend({
	
	validates: {
		balance: {
			presence: true,
			type: "number"
		}
	}
	
});

```

#### email

Validates email addresses

```
var User = Backbone.Model.extend({

	validates: {
		email: {
			type: "email"
		}
	}

});

```

#### size

This validates strings for length and numbers for magnitude. Quoted numbers are validated for magnitude and not length.

```
var User = Backbone.Model.extend({

	validates: {
		first_name: {
			presence: true,
			type: "text",
			size: "> 40" // Validates length
		}
		age: {
			presence: true,
			type: "number",
			size: "> 21" // Validates magnitude
		}
	}
	
});

```

Comparisons supported by size are:

* &gt;
* &lt;
* ==
* &gt;=
* &lt;= 

## Overriding Type Validation

It is possible to provide a custom pattern for an existing type with the ```using``` property.

```
var User = Backbone.Model.extend({
	
	validates: {	
		first_name: {
			type: "text",
			using: /^(\s*([a-zA-Z0-9]+)\s*)$/gi // Now validates alpha-numeric strings
		}
	}
{'fraction_non_alphanumeric': 0.12162723121627231, 'fraction_numerical': 0.0016604400166044002, 'mean_word_length': 3.630365150544523, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3558338', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2269, 'n_words': 805}