Skip to content

Commit

Permalink
Rename validation.failedFields to validation.errors
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
jhnns committed May 11, 2016
1 parent 247b03e commit 93c50ed
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ alamid-schema

If you like [mongoose](http://mongoosejs.com/) schemas and you want to use them standalone, **alamid-schema** is the right module for you.

__alamid-schema__ helps you with
__alamid-schema__ helps you with

- validation of data
- using mongoose-like schemas without using mongoose
Expand All @@ -18,11 +18,11 @@ __alamid-schema__ helps you with
- striping readable/writeable fields (coming soon)

Use it on the server to...
- normalize and validate incoming requests
- normalize and validate incoming requests
- strip private fields from the response
Use it on the client to...
- validate forms

Use it on the client to...
- validate forms
- define view models

```javascript
Expand Down Expand Up @@ -60,7 +60,7 @@ var PandaSchema = new Schema({
});
```

...or with abstract types...
...or with abstract types...

```javascript
var PandaSchema = new Schema({
Expand All @@ -72,7 +72,7 @@ var PandaSchema = new Schema({
});
```

### Extend
### Extend

Sometimes you want to extend your Schema and add new properties.

Expand All @@ -92,7 +92,7 @@ var SuperPanda = PandaSchema.extend("SuperPanda", {
type: Boolean
}
});
```
```

We have a superpanda now... which can fly and has xray eyes!
That's basically the same as...
Expand All @@ -105,16 +105,16 @@ var SuperPanda = new Schema({
type: Array
},
xRay: true, //added
canFly: { //added
canFly: { //added
type: Boolean
}
});
```


__Overwriting properties__
__Overwriting properties__

If you define a property in the schema you are extending with, the extending schema takes precedence.
If you define a property in the schema you are extending with, the extending schema takes precedence.


```javascript
Expand All @@ -135,13 +135,13 @@ equals...
var Panda = new Schema("Panda", {
name: String,
age: Number, //overwritten
color: String //added
color: String //added
});
```

### Plugin: Validation

The validation plugins adds - *suprise!* - validation support.
The validation plugins adds - *suprise!* - validation support.

```javascript
var Schema = require("alamid-schema");
Expand Down Expand Up @@ -185,31 +185,31 @@ PandaSchema.validate(panda, function(validation) {
});
```

outputs...
outputs...

```javascript
{
result: false,
failedFields: {
age: [ 'min' ]
errors: {
age: [ 'min' ]
}
}
```


_Included validators:_
_Included validators:_

- required
- min (works on Number)
- max (works on Number)
- enum
- enum
- minLength (works on String, Array)
- maxLength (works on String, Array)
- hasLength (works on String, Array)

_Writing custom validators:_
You can write sync and async validators..

You can write sync and async validators..

```javascript

Expand Down Expand Up @@ -251,24 +251,24 @@ var panda = {

PandaSchema.validate(panda, function(validation) {
if(!validation.result) {
console.log(validation.failedFields);
console.log(validation.errors);
return;
}
console.log("happy panda");
});
```
```

outputs...
outputs...


```javascript
{
name: [ "name-already-taken" ],
{
name: [ "name-already-taken" ],
age: [ "too-young" ]
}
```

_Note:_ validators will be called with `this` bound to `model`.
_Note:_ validators will be called with `this` bound to `model`.

### Promises

Expand All @@ -283,21 +283,21 @@ PandaSchema.validate(panda)

__Important notice:__ You must bring your own ES6 Promise compatible polyfill!

## API
## API

### Core
### Core

#### Schema(name?: String, definition: Object)

Creates a new schema.
Creates a new schema.

#### .only(key1: Array|String[, key2: String, key3: String, ...])

Returns a subset with the given keys of the current schema. You may pass an array with keys or just the keys as arguments.

#### .extend([name: String, ]definition: Object)

Creates a new schema that inherits from the current schema. Field definitions are merged where appropriate.
Creates a new schema that inherits from the current schema. Field definitions are merged where appropriate.
If a definition conflicts with the parent definition, the child's definition supersedes.

### Readable & Writable fields
Expand Down Expand Up @@ -355,4 +355,4 @@ var PandaReadableSchema = PandaSchema.readable();
#### .validate(model: Object[, callback: Function]): Promise
Validate given model using the schema definitions. Callback will be called/Promise will be fulfilled with a validation object with `result` (Boolean) and `failedFields` (Object) containing the error codes.
Validate given model using the schema definitions. Callback will be called/Promise will be fulfilled with a validation object with `result` (Boolean) and `errors` (Object) containing the error codes.
4 changes: 2 additions & 2 deletions examples/validation/customValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ panda = {

PandaSchema.validate(panda, function (validation) {
if (!validation.result) {
console.log(validation.failedFields);
console.log("failed fields:", Object.keys(validation.failedFields).join(","));
console.log(validation.errors);
console.log("failed fields:", Object.keys(validation.errors).join(","));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/validation/standardValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ PandaSchema = new Schema({

PandaSchema.validate(panda, function (validation) {
if (!validation.result) {
console.log(validation.failedFields);
console.log("failed fields:", Object.keys(validation.failedFields).join(","));
console.log(validation.errors);
console.log("failed fields:", Object.keys(validation.errors).join(","));
return;
}

Expand Down
8 changes: 4 additions & 4 deletions plugins/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function validationPlugin(Schema) {
var result = {
model: model,
result: true,
failedFields: {}
errors: {}
};
var promise;

Expand All @@ -144,12 +144,12 @@ function validationPlugin(Schema) {

self.keys.forEach(function (key) {
pending++;
runValidation(self.validators[key], model[key], model, function (failedFields) {
runValidation(self.validators[key], model[key], model, function (errors) {
pending--;

if (failedFields.length > 0) {
if (errors.length > 0) {
result.result = false;
result.failedFields[key] = failedFields;
result.errors[key] = errors;
}

// was final call
Expand Down
16 changes: 8 additions & 8 deletions test/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe("plugins/validation", function () {

schema.validate({ age: 18 }, function (validation) {
expect(validation.result).to.eql(true);
expect(validation.failedFields).to.eql({});
expect(validation.errors).to.eql({});
done();
});
});
Expand All @@ -269,7 +269,7 @@ describe("plugins/validation", function () {

schema.validate({ age: 18 }, function (validation) {
expect(validation.result).to.eql(true);
expect(validation.failedFields).to.eql({});
expect(validation.errors).to.eql({});
done();
});
});
Expand Down Expand Up @@ -301,7 +301,7 @@ describe("plugins/validation", function () {

return schema.validate({ age: 4 })
.then(function (validation) {
expect(validation).to.eql({ result: true, model: { age: 4 }, failedFields: {} });
expect(validation).to.eql({ result: true, model: { age: 4 }, errors: {} });
});
});

Expand All @@ -319,7 +319,7 @@ describe("plugins/validation", function () {
throw new Error("Should not resolve");
})
.catch(function (validation) {
expect(validation).to.eql({ result: false, model: { age: 1 }, failedFields: { age: ["min"] } });
expect(validation).to.eql({ result: false, model: { age: 1 }, errors: { age: ["min"] } });
});
});
});
Expand Down Expand Up @@ -351,7 +351,7 @@ describe("plugins/validation", function () {
expect(asyncSpy).to.have.been.called.once();
expect(syncSpy).to.have.been.called.once();
expect(validation.result).to.eql(true);
expect(validation.failedFields).to.eql({});
expect(validation.errors).to.eql({});
done();
});
});
Expand All @@ -371,7 +371,7 @@ describe("plugins/validation", function () {
expect(asyncSpy).to.have.been.called.once();
expect(syncSpy).to.have.been.called.once();
expect(validation.result).to.eql(false);
expect(validation.failedFields.age).to.contain("fail-async", "fail-sync");
expect(validation.errors.age).to.contain("fail-async", "fail-sync");
done();
});
});
Expand All @@ -395,7 +395,7 @@ describe("plugins/validation", function () {
expect(asyncSpy).to.have.been.called.once();
expect(syncSpy).to.have.been.called.once();
expect(validation.result).to.eql(false);
expect(validation.failedFields.age).to.contain("fail-async");
expect(validation.errors.age).to.contain("fail-async");
done();
});
});
Expand All @@ -419,7 +419,7 @@ describe("plugins/validation", function () {
expect(asyncSpy).to.have.been.called.once();
expect(syncSpy).to.have.been.called.once();
expect(validation.result).to.equal(false);
expect(validation.failedFields.age).to.contain("fail-sync");
expect(validation.errors.age).to.contain("fail-sync");
done();
});
});
Expand Down

0 comments on commit 93c50ed

Please sign in to comment.