Skip to content

Commit

Permalink
fresh documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 9, 2016
1 parent a5bd829 commit 752a2f4
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 14 deletions.
157 changes: 145 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ var schema = {
var validator = osom(schema)

// schema factory
validator({age: ' 23 '}).should.be.eql({age: '23'})
validator({age: ' 23 '}) // => {age: '23'}
```

## API

### osom(schema, [options])
### osom(schema, [global])

#### schema

Expand Down Expand Up @@ -82,30 +82,163 @@ While in *basic* mode only is possible setup `type` casting, in *advanced* mode

The following keys setup your rule:

- `type`: as in *basic* mode, it specifies the type casting of the output value.
- `default`: whatever default value that you can set if `nill` value as input is provided.
- `filter`: an `Array` collection of data transforms as pipeline of methods to apply for the input value.
###### type

Type: `function`

As in *basic* mode, it specifies the type casting of the output value:

```js
var schema = {
age: {
type: Number
}
}
```

Internally it uses [chaste](https://github.com/Kikobeats/chaste). This makes easy casting compatible types:

```js
osom(schema)({age: '23'}) // => {age: 23}
```

###### casting

Type: `boolean`<br>
Default: `true`

It disables type casting and throws `TypeError` if the `typeof` of the value evaluation is not correct:

```js
var schema = {
age: {
type: String,
casting: false
}
}

osom(schema)({age: '23'}) // => TypeError("Expected a {string} for 'age'.")
```

###### required

Type: `boolean`|`array`<br>
Default: `false`

It marks a rule as required field and throws `TypeError` if value for the field is not present.

If you want to provide a custom error message, provide an `Array` where the second element represent the message:

```js
var schema = {
age: {
type: String,
required: [true, 'your message here']
}
}
```

###### default

Type: `whatever`|`function`<br>
Default: `null`

It sets a default value if `nill` value as input is provided.

Additionally you can provide a `Function` for set a dynamic value:

```js
var schema = {
age: {
type: Number, default: function () { return 23 }
}
}
```

###### transform

Type: `array`<br>
Default: `[]`

An `Array` collection of data transforms as pipeline of methods to apply for the input value:

```js
function trim (str) {
return str.trim()
}

var advancedSchema = {
var schema = {
age: {
type: String,
default: '23',
filter: [trim]
transform: [trim]
}
}
```

#### options
###### validate

Type: `object`
Default: `soon`
Type: `function`|`object`<br>
Default: `null`

It setup a `Function` that will be exec to validate the input value and if it fails, it throws `TypeError`.

```js
var schema = {
age: {
type: String,
validate: function (v) {
return v === '23'
}
}
}

osom(schema)({age: 25}) // => TypeError("Fail '25' validation for 'age'.")
```

Providing a object brings you the possibility set up a custom error message:


```js
var schema = {
age: {
type: String,
validate: {
validator: function (v) {
return v === '23'
},
message: 'expected a millenial value instead of {VALUE}!'
}
}
}

osom(schema)({age: 25}) // => TypeError("expected a millenial value instead of 25!")
```

#### global

Type: `object`<br>
Default: `{}`

*soon*
Meanwhile is possible provide specific options per each rule, also is possible provide them as global to apply to all rule. This minimizes the schemas definitions:

```js
function trim (str) {
return str.trim()
}

var schema = {
age: {
type: String
}
}

var globalFields = {
transform: [trim]
}

var validator = osom(schema, globalFields)
validator({age: ' 23 '}) // => {age: '23'}
```

## License

Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ describe('schema defintion', function () {
validator: function (v) {
return v === '23'
},
message: 'expected a millenial instead of {VALUE}!'
message: 'expected a millenial value instead of {VALUE}!'
}
}
}

var validator = osom(schema)
var errMessage = 'expected a millenial instead of 25!'
var errMessage = 'expected a millenial value instead of 25!'
;(function () { validator({age: 25}) }).should.throw(errMessage)
})
})
Expand Down

0 comments on commit 752a2f4

Please sign in to comment.