Skip to content

Commit 0f3da06

Browse files
author
Umed Khudoiberdiev
committed
initial commit
0 parents  commit 0f3da06

21 files changed

+1898
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### OS
2+
3+
Thumbs.db
4+
ehthumbs.db
5+
Desktop.ini
6+
$RECYCLE.BIN/
7+
.DS_Store
8+
.AppleDouble
9+
.LSOverride
10+
Icon
11+
._*
12+
.Spotlight-V100
13+
.Trashes
14+
15+
### IDE
16+
17+
.idea/
18+
19+
### Project
20+
21+
typings/
22+
node_modules/
23+
built/
24+
npm-debug.log

README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Type Validator
2+
3+
Wrapper over [validator.js][1] library that provides you easy way to use it with Typescript classes.
4+
5+
## Usage
6+
7+
Create your class and put some validation annotations on its properties you want to validate:
8+
9+
```typescript
10+
import {Contains, IsInt, IsLength, IsEmail, IsFQDN, IsDate} from "type-validator/ValidationAnnotations";
11+
12+
export class Post {
13+
14+
@IsLength(10, 20)
15+
title: string;
16+
17+
@Contains('hello')
18+
text: string;
19+
20+
@IsInt({ min: 0, max: 10 })
21+
rating: number;
22+
23+
@IsEmail()
24+
email: string;
25+
26+
@IsFQDN()
27+
site: string;
28+
29+
@IsDate()
30+
createDate: Date;
31+
32+
}
33+
34+
let validator = new Validator();
35+
36+
let post = new Post();
37+
post.title = 'Hello'; // should not pass
38+
post.text = 'this is a great post about hell world'; // should not pass
39+
post.rating = 11; // should not pass
40+
post.email = 'google.com'; // should not pass
41+
post.site = 'googlecom'; // should not pass
42+
43+
console.log(validator.validate(Post, post)); // returns you array of errors for fields that didn't pass validation
44+
```
45+
46+
Validator also supports validation groups.
47+
Take a look on samples in `./sample` for more examples of usages.
48+
49+
## Todos
50+
51+
* cover with tests
52+
* more documentation and samples
53+
* add support to load validation configuration from json and plain javascript objects
54+
* add support to work with vanila js
55+
56+
[1]: https://github.com/chriso/validator.js

gulpfile.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var gulp = require('gulp');
2+
var runSequence = require('run-sequence');
3+
var del = require('del');
4+
var plumber = require('gulp-plumber');
5+
var ts = require('gulp-typescript');
6+
var exec = require('gulp-exec');
7+
8+
gulp.task('clean', function (cb) {
9+
return del(['./built/**'], cb);
10+
});
11+
12+
gulp.task('compile', function() {
13+
var tsProject = ts.createProject('tsconfig.json', {
14+
sortOutput: true,
15+
typescript: require('typescript')
16+
});
17+
return tsProject.src()
18+
.pipe(plumber())
19+
.pipe(ts(tsProject))
20+
.js.pipe(gulp.dest('./built/es5'));
21+
});
22+
23+
gulp.task('tsd', function() {
24+
return gulp.src('./')
25+
.pipe(exec('./node_modules/.bin/tsd install'))
26+
.pipe(exec('./node_modules/.bin/tsd rebundle'))
27+
.pipe(exec('./node_modules/.bin/tsd link'))
28+
.pipe(exec.reporter());
29+
});
30+
31+
gulp.task('build-package-copy-src', function() {
32+
return gulp.src('./built/es5/src/**/*')
33+
.pipe(gulp.dest('./built/package'));
34+
});
35+
36+
gulp.task('build-package-copy-package-files', function() {
37+
return gulp.src(['./package.json', 'README.md'])
38+
.pipe(gulp.dest('./built/package'));
39+
});
40+
41+
gulp.task('build-package-generate-dts', function () {
42+
var fs = require('fs');
43+
function getFiles (dir, files){
44+
files = files || [];
45+
var filesInDir = fs.readdirSync(dir);
46+
for (var i in filesInDir){
47+
var name = dir + '/' + filesInDir[i];
48+
if (fs.statSync(name).isDirectory()){
49+
getFiles(name, files);
50+
} else {
51+
files.push(name);
52+
}
53+
}
54+
return files;
55+
}
56+
57+
var dtsGenerator = require('dts-generator');
58+
var name = require('./package.json').name;
59+
var files = getFiles('./src');
60+
dtsGenerator.generate({
61+
name: name,
62+
baseDir: './src',
63+
files: files,
64+
out: './built/package/' + name + '.d.ts'
65+
});
66+
});
67+
68+
gulp.task('build', function(cb) {
69+
return runSequence(
70+
'clean',
71+
'tsd',
72+
'compile',
73+
cb
74+
);
75+
});
76+
77+
gulp.task('package', function(cb) {
78+
return runSequence(
79+
'build',
80+
['build-package-copy-src', 'build-package-copy-package-files', 'build-package-generate-dts'],
81+
cb
82+
);
83+
});
84+
85+
gulp.task('default', ['build']);

package.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "type-validator",
3+
"version": "0.0.1",
4+
"description": "Wrapper over validator.js library that allows to use validation decorators in your Typescript classes",
5+
"license": "Apache-2.0",
6+
"readmeFilename": "README.md",
7+
"author": {
8+
"name": "Umed Khudoiberdiev",
9+
"email": "zarrhost@gmail.com"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/PLEEROCK/type-validator.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/PLEEROCK/type-validator/issues"
17+
},
18+
"tags": [
19+
"validator",
20+
"validation",
21+
"typescript",
22+
"typescript-validator"
23+
],
24+
"typings": "./type-validator.d.ts",
25+
"typescript": {
26+
"definitions": [
27+
"type-validator.d.ts"
28+
]
29+
},
30+
"dependencies": {
31+
"validator": "^4.1.0"
32+
},
33+
"devDependencies": {
34+
"del": "^2.0.2",
35+
"dts-generator": "^1.5.0",
36+
"fs": "0.0.2",
37+
"gulp": "^3.9.0",
38+
"gulp-exec": "^2.1.1",
39+
"gulp-plumber": "^1.0.1",
40+
"gulp-typescript": "^2.9.0",
41+
"run-sequence": "^1.1.4",
42+
"tsd": "^0.6.4",
43+
"typescript": "^1.6.2"
44+
},
45+
"scripts": {
46+
"postversion": "./node_modules/.bin/gulp package"
47+
}
48+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Contains, IsInt, IsLength, IsEmail, IsFQDN, IsDate} from "../../src/annotation/ValidationAnnotations";
2+
3+
export class Post {
4+
5+
@IsLength(10, 20)
6+
title: string;
7+
8+
@Contains('hello')
9+
text: string;
10+
11+
@IsInt({ min: 0, max: 10 })
12+
rating: number;
13+
14+
@IsEmail()
15+
email: string;
16+
17+
@IsFQDN()
18+
site: string;
19+
20+
@IsDate()
21+
createDate: Date;
22+
23+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {Validator} from "../../src/Validator";
2+
import {Post} from "./Post";
3+
4+
let validator = new Validator();
5+
6+
// Sample1. simple validation
7+
8+
let post1 = new Post();
9+
post1.title = 'Hello world'; // should pass
10+
post1.text = 'this is a great post about hello world'; // should pass
11+
post1.rating = 10; // should pass
12+
post1.email = 'info@google.com'; // should pass
13+
post1.site = 'google.com'; // should pass
14+
post1.createDate = new Date(); // should pass
15+
16+
console.log(validator.validate(Post, post1)); // should pass completely, e.g. return empty array
17+
18+
let post2 = new Post();
19+
post2.title = 'Hello'; // should not pass
20+
post2.text = 'this is a great post about hell world'; // should not pass
21+
post2.rating = 11; // should not pass
22+
post2.email = 'google.com'; // should not pass
23+
post2.site = 'googlecom'; // should not pass
24+
// should not pass because date property is missing
25+
26+
console.log(validator.validate(Post, post2)); // should not pass completely, must return array of ValidationError-s
27+
28+
// Sample2. using validation options to skip properties that are not defined
29+
30+
let post3 = new Post();
31+
post3.title = 'Hello'; // should not pass
32+
post3.text = 'this is a great post about hell world'; // should not pass
33+
post3.rating = 11; // should not pass
34+
post3.email = 'google.com'; // should not pass
35+
post3.site = 'googlecom'; // should not pass
36+
37+
console.log(validator.validate(Post, post3, { skipMissingProperties: true })); // should not pass, but returned ValidationError-s should not have error about date field
38+
39+
let post4 = new Post();
40+
post4.title = 'Hello world'; // should pass
41+
post4.text = 'this is a great post about hello world'; // should pass
42+
post4.rating = 10; // should pass
43+
post4.email = 'info@google.com'; // should pass
44+
post4.site = 'google.com'; // should pass
45+
46+
console.log(validator.validate(Post, post4, { skipMissingProperties: true })); // should pass even if date is not set
47+
48+
// Sample3. using validation groups
49+
50+
let post5 = new Post();
51+
post5.title = 'Hello world'; // should pass
52+
post5.text = 'this is a great post about hello world'; // should pass
53+
post5.rating = 10; // should pass
54+
post5.email = 'info@google.com'; // should pass
55+
post5.site = 'google.com'; // should pass
56+
57+
console.log(validator.validate(Post, post5, { skipMissingProperties: true })); // should pass even if date is not set
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Trim, Rtrim, ToInt, Blacklist} from "../../src/annotation/SanitizeAnnotations";
2+
3+
export class Post {
4+
5+
@Trim()
6+
title: string;
7+
8+
@Rtrim(['.'])
9+
@Blacklist(/(1-9)/)
10+
text: string;
11+
12+
@ToInt()
13+
rating: number;
14+
15+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Validator} from "../../src/Validator";
2+
import {Post} from "./Post";
3+
4+
let validator = new Validator();
5+
6+
// Sample1. simple sanitization
7+
8+
let post1 = new Post();
9+
post1.title = ' Hello world '; // should pass
10+
post1.text = '1. this is a great (2) post about hello 3 world.'; // should pass
11+
post1.rating = 12.2; // should pass
12+
13+
validator.sanitize(Post, post1);
14+
console.log(post1);

sample/sample3-using-groups/Post.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Contains, IsInt, IsLength, IsEmail, IsFQDN, IsDate} from "../../src/annotation/ValidationAnnotations";
2+
3+
export class Post {
4+
5+
@IsLength(10, 20, {
6+
message: 'Incorrect length!',
7+
groups: ['users', 'moderators']
8+
})
9+
@IsLength(0, 20, {
10+
message: 'Incorrect length!',
11+
groups: ['admins']
12+
})
13+
title: string;
14+
15+
@Contains('hello', {
16+
message: 'It should contain word "hello!"',
17+
groups: ['users', 'moderators']
18+
})
19+
text: string;
20+
21+
@IsInt({ min: 0, max: 10 })
22+
rating: number;
23+
24+
@IsEmail(null, {
25+
always: true
26+
})
27+
email: string;
28+
29+
@IsFQDN(null, {
30+
message: 'Site address should be correct',
31+
groups: ['users']
32+
})
33+
site: string;
34+
35+
@IsDate()
36+
createDate: Date;
37+
38+
}

0 commit comments

Comments
 (0)