Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

correct

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

correct - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

src/number/descriptor.js

4

package.json
{
"name": "correct",
"version": "0.0.2",
"version": "0.1.0",
"description": "Simple JavaScript validation module",

@@ -32,4 +32,4 @@ "main": "index.js",

"i-s": "~1.0.2",
"ustring": "~1.2.1"
"ustring": "~1.3.0"
}
}

@@ -5,1 +5,127 @@ correct

Simple JavaScript validation module
## Install
```npm install correct```
## Usage
```js
var correct = require('correct')
var fn = correct.string.min(1).max(12)
fn('abc') === true
fn('') === false
```
At any time, if you want to understand why you have a failing validation, you can use
```js
var fn = correct.string.min(1).max(12)
fn.validate('')
// will return an object with {valid: false, errors: [...]} and the array of validations that did not pass
fn.validate('abc') //will return {valid: true}
```
## Types
For now there are only 3 base validation types available
* string - correct.string
* number - correct.number
* int - correct.int
Example:
```js
var isInt = correct.int
isInt('5') == false
isInt(5.1) == false
isInt(5) == true
```
You can chain validation functions for all types (detailed below).
```js
var intMax10 = correct.int.max(10)
intMax10(4) == true
intMax10(40) == false
```
NOTE: types are specified with simple dot access, while all other chained validations should be chained as function calls:
```js
var validate = correct.string.max(10).not('hello word').required()
validate('my string') == true
```
### string
```js
var stringMin5 = correct.string.min(5)
stringMin5('abc') == false
stringMin5('abcde') == true
```
Available string tests:
* min(n: Number) - validates the value to have at least n characters in length
* max(n: Number) - validates the value to have at most n characters in length
* re(r: RegExp) - validates the value against the given regular expression
* email() - validates the value to be a valid email address (npm module isemail is used)
* fn(f: Function) - validates the value if the specified function returns truthy
* not(v: String) - validates the value if it is not strict equal to v
* numeric() - validates the value if it is a numeric string: eg - '1.34'
* required() - validates the value if it is different from empty string
### Number
Validates numbers.
```js
var between10And100 = correct.number.min(10).max(100)
between10And100(40) == true
between10And100(4) == false
var even = correct.number.fn(function(v){
return v % 2 == 0
})
even(2) == true
var x = 67
even(x) == false
var isNumber = correct.number
isNumber(4.5) == true
isNumber('4.5') == false
```
Available number tests:
* min(n: Number) - validates the value if it is >= n
* max(n: Number) - validates the value if it is <= n
* fn(f: Function) - validates the value if the specified function returns truthy
* not(v: String) - validates the value if it is not strict equal to v
### Int
Inherits all the number tests, and adds some more
* odd(n: Number) - validates the value if it is odd
* even(n: Number) - validates the value if it is even
## Tests
```make```
## License
```MIT```

@@ -8,2 +8,3 @@ var is = require('i-s')

require('./text')(defineType)
require('./number')(defineType)
require('./int')(defineType)

@@ -10,0 +11,0 @@

@@ -1,57 +0,34 @@

var is = require('ustring').is
var d = require('../number/descriptor')
function isMin(min){
this.addDescription({
min: min
})
return function(value){
return value >= min
}
function even(value){
return value % 2 == 0
}
function isMax(max){
function isOdd(){
this.addDescription({
max: max
odd: true
})
return function(value){
return value <= max
return !even(value)
}
}
function isRequired(){
function isEven(){
this.addDescription({
required: true
even: true
})
return function(value){
return value != ''
return even(value)
}
}
function isRe(re){
this.addDescription({
regexp: re.toString()
})
return function(value){
return typeof value == 'string' && !!re.test(value)
}
d.odd = function(){
return this._add(isOdd.call(this))
}
d.even = function(){
return this._add(isEven.call(this))
}
module.exports = {
min: function(min){
return this._add(isMin.call(this, min))
},
max: function(max){
return this._add(isMax.call(this, max))
},
required: function(){
return this._add(isRequired.call(this))
},
re: function(re){
return this._add(isRe.call(this, re))
}
}
module.exports = d
var is = require('ustring').is
var validations = require('../validations')

@@ -40,2 +41,21 @@ function isMinLen(min){

function isNumeric(){
this.addDescription({
numeric: true
})
return is.numeric
}
function isRequired(){
this.addDescription({
required: true
})
return function(value){
return value != ''
}
}
function isRegexp(re){

@@ -61,3 +81,11 @@ this.addDescription({

return this._add(isRegexp.call(this, r))
}
},
required: function(){
return this._add(isRequired.call(this))
},
numeric: function(){
return this._add(isNumeric.call(this))
},
not: validations.not,
fn: validations.fn
}

@@ -22,2 +22,18 @@ describe('String validation', function(){

it('should make string required', function(){
var req = isString.required()
req('')
.should
.equal(false)
req('a')
.should
.equal(true)
req()
.should
.equal(false)
})
it('should validate string with length >= min', function(){

@@ -122,2 +138,36 @@ var min5 = correct.string.min(5)

})
it('should validate fn', function(){
var valid = isString.fn(function(v){
return v.indexOf('aa') == 0
})
valid('abc')
.should
.equal(false)
valid('aabc')
.should
.equal(true)
})
it('should validate not', function(){
var valid = isString.not('a')
valid('a')
.should
.equal(false)
valid('aa')
.should
.equal(true)
})
it('should validate numeric', function(){
var valid = isString.numeric()
valid('4.5')
.should
.equal(true)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc