Skip to content

Commit d38e97c

Browse files
committed
Fixed Readme file. Was still using lowercase minlength instead of minLength in examples.
1 parent 207bc96 commit d38e97c

9 files changed

+843
-836
lines changed

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
custom: https://www.buymeacoffee.com/ybouane
1+
custom: https://www.buymeacoffee.com/ybouane

.gitignore

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
.DS_Store
2-
node_modules
3-
4-
package-lock.json
5-
yarn.lock
6-
7-
8-
logo.svg
1+
.DS_Store
2+
node_modules
3+
4+
package-lock.json
5+
yarn.lock
6+
7+
8+
logo.svg

CHANGELOG.md

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# Changelog
2-
All notable changes to this project will be documented in this file.
3-
4-
## [unreleased]
5-
- TODO
6-
7-
## [1.4.0] - 2022-03-14
8-
### Added
9-
- Added Mixed type to ElValidator for validating mixed inputs.
10-
- Added Types static property to ElValidator.
11-
12-
## [1.3.0] - 2022-03-06
13-
### Fixed
14-
- Fixed minLength not being registered during checkSchema step.
15-
16-
## [1.2.0] - 2022-03-06
17-
### Changed
18-
- Renamed minlength and maxlength options to minLength and maxLength to match mongoose's schema.
19-
20-
## [1.1.0] - 2022-03-06
21-
### Fixed
22-
- Schema checker bug solved where name/type fields were being overwritten in the case of a sub-schema.
23-
24-
## [1.0.0] - 2022-03-05
25-
### Added
26-
- Initial implementation of the library
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
## [unreleased]
5+
- TODO
6+
7+
## [1.5.0] - 2022-11-25
8+
### Fixed
9+
- Fixed Readme file. Was still using lowercase minlength instead of minLength in examples.
10+
11+
## [1.4.0] - 2022-03-14
12+
### Added
13+
- Added Mixed type to ElValidator for validating mixed inputs.
14+
- Added Types static property to ElValidator.
15+
16+
## [1.3.0] - 2022-03-06
17+
### Fixed
18+
- Fixed minLength not being registered during checkSchema step.
19+
20+
## [1.2.0] - 2022-03-06
21+
### Changed
22+
- Renamed minlength and maxlength options to minLength and maxLength to match mongoose's schema.
23+
24+
## [1.1.0] - 2022-03-06
25+
### Fixed
26+
- Schema checker bug solved where name/type fields were being overwritten in the case of a sub-schema.
27+
28+
## [1.0.0] - 2022-03-05
29+
### Added
30+
- Initial implementation of the library

example.js

+88-88
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
//import ElValidator from 'elvalidator';
2-
import ElValidator from './index.js';
3-
4-
// Set up the validator object
5-
let validator = new ElValidator({
6-
name : { type: String, required:true, trim:true, minLength:3 },
7-
age : { type: Number, required:true, integer:true, min:18, max:100 },
8-
agreedTelemetry : { type: Boolean, default:false },
9-
10-
// Array example:
11-
tags : [
12-
{ type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ }
13-
],
14-
15-
// Object example:
16-
settings : {
17-
darkMode : { type: Boolean, required: true, },
18-
codeEditor : { type: String, default:'atom', enum:['atom', 'vstudio', 'notepad++'] },
19-
extras : { type: Object, required:true, }
20-
},
21-
stringOrBool : {
22-
$or : [
23-
{ type: String },
24-
{ type: Boolean },
25-
]
26-
},
27-
arrayWithOptions : {
28-
type : [
29-
{ type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ }
30-
],
31-
minEntries : 0,
32-
maxEntries : 20,
33-
uniqueValues : true,
34-
},
35-
custom : { type: String, validator: async (value) => {
36-
if(value.length%2 == 1)
37-
throw new Error('The number of characters must be odd.');
38-
return '->'+value;
39-
}},
40-
}, {
41-
strictMode : true,
42-
throwUnkownFields : false,
43-
accumulateErrors : false,
44-
});
45-
46-
// Validate/Sanitize a data object
47-
var sanitizedData = await validator.validate({
48-
name : 'Yassine',
49-
age : 27,
50-
51-
tags : ['programmer', 'javascript'],
52-
settings : {
53-
darkMode : false,
54-
//codeEditor : 'atom',
55-
extras : {
56-
one : '1',
57-
two : 'anything',
58-
}
59-
},
60-
other : 'unknown field',
61-
stringOrBool : false,
62-
arrayWithOptions : ['hello', 'world', 'tag', 'WillDownCase', 'TAG', 'last'],
63-
custom : 'node'
64-
});
65-
66-
console.log(sanitizedData);
67-
68-
69-
70-
/*
71-
OUTPUT:
72-
73-
{
74-
name: 'Yassine',
75-
age: 27,
76-
agreedTelemetry: false,
77-
tags: [ 'programmer', 'javascript' ],
78-
settings: {
79-
darkMode: false,
80-
codeEditor: 'atom',
81-
extras: { one: '1', two: 'anything' }
82-
},
83-
stringOrBool: false,
84-
arrayWithOptions: [ 'hello', 'world', 'tag', 'willdowncase', 'last' ],
85-
custom: '->node'
86-
}
87-
88-
*/
1+
//import ElValidator from 'elvalidator';
2+
import ElValidator from './index.js';
3+
4+
// Set up the validator object
5+
let validator = new ElValidator({
6+
name : { type: String, required:true, trim:true, minLength:3 },
7+
age : { type: Number, required:true, integer:true, min:18, max:100 },
8+
agreedTelemetry : { type: Boolean, default:false },
9+
10+
// Array example:
11+
tags : [
12+
{ type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ }
13+
],
14+
15+
// Object example:
16+
settings : {
17+
darkMode : { type: Boolean, required: true, },
18+
codeEditor : { type: String, default:'atom', enum:['atom', 'vstudio', 'notepad++'] },
19+
extras : { type: Object, required:true, }
20+
},
21+
stringOrBool : {
22+
$or : [
23+
{ type: String },
24+
{ type: Boolean },
25+
]
26+
},
27+
arrayWithOptions : {
28+
type : [
29+
{ type: String, minLength:3, lowercase:true, trim:true, match: /^[a-z0-9]+$/ }
30+
],
31+
minEntries : 0,
32+
maxEntries : 20,
33+
uniqueValues : true,
34+
},
35+
custom : { type: String, validator: async (value) => {
36+
if(value.length%2 == 1)
37+
throw new Error('The number of characters must be odd.');
38+
return '->'+value;
39+
}},
40+
}, {
41+
strictMode : true,
42+
throwUnkownFields : false,
43+
accumulateErrors : false,
44+
});
45+
46+
// Validate/Sanitize a data object
47+
var sanitizedData = await validator.validate({
48+
name : 'Yassine',
49+
age : 27,
50+
51+
tags : ['programmer', 'javascript'],
52+
settings : {
53+
darkMode : false,
54+
//codeEditor : 'atom',
55+
extras : {
56+
one : '1',
57+
two : 'anything',
58+
}
59+
},
60+
other : 'unknown field',
61+
stringOrBool : false,
62+
arrayWithOptions : ['hello', 'world', 'tag', 'WillDownCase', 'TAG', 'last'],
63+
custom : 'node'
64+
});
65+
66+
console.log(sanitizedData);
67+
68+
69+
70+
/*
71+
OUTPUT:
72+
73+
{
74+
name: 'Yassine',
75+
age: 27,
76+
agreedTelemetry: false,
77+
tags: [ 'programmer', 'javascript' ],
78+
settings: {
79+
darkMode: false,
80+
codeEditor: 'atom',
81+
extras: { one: '1', two: 'anything' }
82+
},
83+
stringOrBool: false,
84+
arrayWithOptions: [ 'hello', 'world', 'tag', 'willdowncase', 'last' ],
85+
custom: '->node'
86+
}
87+
88+
*/

example2.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
2-
3-
// Without ElValidator
4-
5-
if(!Object.prototype.hasOwnProperty.call(req.body, 'name'))
6-
throw new Error('The "Name" field is required.');
7-
if(typeof req.body.name!=='string')
8-
throw new Error('The "Name" field must be a text field.');
9-
if(req.body.name.length<3)
10-
throw new Error('The "Name" field must have at least 3 characters.');
11-
12-
if(!Object.prototype.hasOwnProperty.call(req.body, 'tags'))
13-
throw new Error('The "Tags" field is required.');
14-
if(!Array.isArray(req.body.tags))
15-
throw new Error('The "Tags" field must be an array.');
16-
if(req.body.tags.some(tag=> {
17-
return typeof tag!='string' || tag.length<2 || !tag.match(/[a-z0-9_]+/);
18-
}))
19-
throw new Error('The "Tags" field has invalid value(s).');
20-
21-
req.body.tags = req.body.tags.map(tag=>tag.toLowerCase());
22-
23-
24-
25-
// With ElValidator
26-
let sanitized = await ElValidator.validate(req.body, {
27-
name : { type: String, required: true, minLength:3 },
28-
tags : [
29-
{ type: String, required:true, lowercase:true, minLength:2, match:/[a-z0-9_]+/ }
30-
],
31-
})
1+
2+
3+
// Without ElValidator
4+
5+
if(!Object.prototype.hasOwnProperty.call(req.body, 'name'))
6+
throw new Error('The "Name" field is required.');
7+
if(typeof req.body.name!=='string')
8+
throw new Error('The "Name" field must be a text field.');
9+
if(req.body.name.length<3)
10+
throw new Error('The "Name" field must have at least 3 characters.');
11+
12+
if(!Object.prototype.hasOwnProperty.call(req.body, 'tags'))
13+
throw new Error('The "Tags" field is required.');
14+
if(!Array.isArray(req.body.tags))
15+
throw new Error('The "Tags" field must be an array.');
16+
if(req.body.tags.some(tag=> {
17+
return typeof tag!='string' || tag.length<2 || !tag.match(/[a-z0-9_]+/);
18+
}))
19+
throw new Error('The "Tags" field has invalid value(s).');
20+
21+
req.body.tags = req.body.tags.map(tag=>tag.toLowerCase());
22+
23+
24+
25+
// With ElValidator
26+
let sanitized = await ElValidator.validate(req.body, {
27+
name : { type: String, required: true, minLength:3 },
28+
tags : [
29+
{ type: String, required:true, lowercase:true, minLength:2, match:/[a-z0-9_]+/ }
30+
],
31+
})

0 commit comments

Comments
 (0)