Skip to content

Commit aadb457

Browse files
Merge pull request #12 from FieldVal/develop
Changed error structure to use only "invalid"
2 parents d436090 + 395f0aa commit aadb457

38 files changed

+1197
-668
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fieldval",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"main": "fieldval.js",
55
"ignore": [
66
"coverage",

docs/fieldval.json

Lines changed: 50 additions & 11 deletions
Large diffs are not rendered by default.

docs_src/Async/Async.code

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var validator = new FieldVal({
2+
my_number: 56,
3+
my_email: "test@example.com"
4+
});
5+
6+
validator.get_async(
7+
"my_email",
8+
[
9+
BasicVal.email({required: true}),
10+
function(val,emit,done){
11+
setTimeout(function(){
12+
done({
13+
"error_message": "I couldn't find that email address"
14+
})
15+
},500);
16+
}
17+
],
18+
function(my_email){
19+
console.log("my_email: ",my_email);
20+
}
21+
);
22+
23+
var my_number = validator.get("my_number", BasicVal.number({required:true}));
24+
25+
validator.end(function(error){
26+
console.log(error);
27+
});

docs_src/Async/Async.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Requiring an asynchronous action to validate a parameter is a common validation scenario.
2+
3+
To use asynchronous checks, call ```.get_async(key_name, check_array, [callback])``` on the FieldVal instance.
4+
5+
```check_array``` is an array of checks and ```callback``` is a function to call after validation has finished.
6+
7+
You can use synchronous ```.get``` and asynchronous ```.get_async``` within the same FieldVal instance.
8+
9+
Synchronous checks work with ```.get_async```, but asynchronous checks will not work with ```.get```.
10+
11+
The ```.end``` function of the FieldVal instance takes a callback that will be called with the error if one is present.
12+
13+
```.end``` waits for all fields to finish validating before calling the callback.

docs_src/Async/Async_runnable.code

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var validator = new FieldVal({
2+
my_number: 56,
3+
my_email: "test@example.com"
4+
});
5+
6+
validator.get_async(
7+
"my_email",
8+
[
9+
BasicVal.email({required: true}),
10+
function(val,emit,done){
11+
setTimeout(function(){
12+
done({
13+
"error_message": "I couldn't find that email address"
14+
});
15+
},500);
16+
}
17+
],
18+
function(my_email){
19+
var element = document.createElement("div");
20+
element.innerHTML = "my_email: "+my_email
21+
document.body.appendChild(element);
22+
}
23+
);
24+
25+
var my_number = validator.get("my_number", BasicVal.number({required:true}));
26+
27+
validator.end(function(error){
28+
var element = document.createElement("pre");
29+
element.innerHTML = JSON.stringify(error, null, 4);
30+
document.body.appendChild(element);
31+
});

docs_src/BasicVal/OtherChecks/each/each.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
```BasicVal.each(on_each, [flags])```
22

3-
Iterates through the array value and calls ```on_each``` function on each value.
4-
53
Iterates through the provided array and calls the ```on_each``` function on each array's value.
64

75
```on_each(value, index, emit)```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//CODE
2+
var validator = new FieldVal({
3+
an_array: [44, 56, 3]
4+
});
5+
6+
validator.get_async(
7+
'an_array',
8+
[
9+
BasicVal.array(true),
10+
BasicVal.each_async(function(val,index,emit,done){
11+
setTimeout(function(){
12+
var error = BasicVal.integer().check(val);
13+
if(error){
14+
done(error);
15+
return;
16+
}
17+
18+
done(BasicVal.minimum(40).check(val));
19+
},1);
20+
})
21+
]
22+
)
23+
24+
validator.end(function(error){
25+
var element = document.createElement("pre");
26+
element.innerHTML = JSON.stringify(error, null, 4);
27+
document.body.appendChild(element);
28+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```BasicVal.each_async(on_each, [flags])```
2+
3+
Iterates through the provided array and calls the ```on_each``` function on each array's value. The ```on_each``` function for ```each_async``` uses a callback to provide its response.
4+
5+
```on_each(value, index, emit, done)```
6+
7+
The provided function should call ```done``` with a FieldVal [error](/docs/fieldval/Errors) or with no arguments if there are no errors.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var validator = new FieldVal({
2+
an_array: [44, 56, 3]
3+
});
4+
5+
validator.get_async(
6+
'an_array',
7+
[
8+
BasicVal.array(true),
9+
BasicVal.each_async(function(val,index,emit,done){
10+
setTimeout(function(){
11+
var error = BasicVal.integer().check(val);
12+
if(error){
13+
done(error);
14+
return;
15+
}
16+
17+
done(BasicVal.minimum(40).check(val));
18+
},1);
19+
})
20+
]
21+
)
22+
23+
validator.end(function(error){
24+
var element = document.createElement("pre");
25+
element.innerHTML = JSON.stringify(error, null, 4);
26+
document.body.appendChild(element);
27+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var validator = new FieldVal({
2+
value: 15//Change to 20 to successfully pass .minimum check
3+
});
4+
5+
validator.get_async(
6+
'value',
7+
[
8+
BasicVal.multiple_async([
9+
[
10+
BasicVal.integer(),function(val,emit,done){
11+
setTimeout(function(){
12+
done(BasicVal.minimum(20).check(val));
13+
},1);
14+
}
15+
],
16+
[
17+
BasicVal.prefix("abc")
18+
]
19+
])
20+
]
21+
)
22+
23+
validator.end(function(error){
24+
console.log(error);
25+
});
26+

0 commit comments

Comments
 (0)