Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit b783b0f

Browse files
feat(rules): Added Mimetype rule
1 parent c56cb97 commit b783b0f

11 files changed

Lines changed: 110 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
121121
- [Luhn](docs/luhn.md)
122122
- [MacAddress](docs/mac-address.md)
123123
- [Max](docs/max.md)
124+
- [Mimetype](docs/mimetype.md)
124125
- [Min](docs/min.md)
125126
- [Multiple](docs/multiple.md)
126127
- [Negative](docs/negative.md)

awesome-validator.min.js

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/mimetype.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Mimetype
2+
3+
Validates if the file mimetype matches the expected one.
4+
5+
Valid values:
6+
7+
```js
8+
validator.mimetype('application/javascript').validate('js');
9+
validator.mimetype('application/json').validate('json');
10+
validator.mimetype('text/plain').validate('txt');
11+
```
12+
13+
Invalid values:
14+
15+
```js
16+
validator.mimetype('foo').validate('bar');
17+
validator.mimetype('txt').validate('foo/txt');
18+
```

package-lock.json

Lines changed: 24 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
}
5959
},
6060
"dependencies": {
61+
"@types/mime-types": "^2.1.0",
62+
"mime-types": "^2.1.17",
6163
"moment": "^2.19.3"
6264
},
6365
"devDependencies": {

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export * from './lowercase';
4242
export * from './luhn';
4343
export * from './mac-address';
4444
export * from './max';
45+
export * from './mimetype';
4546
export * from './min';
4647
export * from './multiple';
4748
export * from './negative';

src/rules/mimetype.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { lookup } from 'mime-types';
2+
3+
import { AbstractRule } from './abstract-rule';
4+
5+
export class Mimetype extends AbstractRule {
6+
7+
/**
8+
* Mimetype.
9+
*/
10+
public constructor(public readonly mimetype: string) {
11+
super();
12+
}
13+
14+
/**
15+
* Validate.
16+
*/
17+
public validate(input: any): boolean {
18+
return lookup(input) === this.mimetype;
19+
}
20+
}

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class Validator extends AllOf {
5252
public static luhn = (): Validator => new Validator(new rules.Luhn());
5353
public static macAddress = (): Validator => new Validator(new rules.MacAddress());
5454
public static max = (interval?: any, inclusive: boolean = true): Validator => new Validator(new rules.Max(interval, inclusive));
55+
public static mimetype = (mimetype: string): Validator => new Validator(new rules.Mimetype(mimetype));
5556
public static min = (interval?: any, inclusive: boolean = true): Validator => new Validator(new rules.Min(interval, inclusive));
5657
public static multiple = (multipleOf: number): Validator => new Validator(new rules.Multiple(multipleOf));
5758
public static negative = (): Validator => new Validator(new rules.Negative());
@@ -160,6 +161,7 @@ export class Validator extends AllOf {
160161
public luhn = (): this => this.addRule(new rules.Luhn());
161162
public macAddress = (): this => this.addRule(new rules.MacAddress());
162163
public max = (interval?: any, inclusive: boolean = true): this => this.addRule(new rules.Max(interval, inclusive));
164+
public mimetype = (mimetype: string): this => this.addRule(new rules.Mimetype(mimetype));
163165
public min = (interval?: any, inclusive: boolean = true): this => this.addRule(new rules.Min(interval, inclusive));
164166
public multiple = (multipleOf: number): this => this.addRule(new rules.Multiple(multipleOf));
165167
public negative = (): this => this.addRule(new rules.Negative());

test/rules/mimetype.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Mimetype } from '../../src/rules/mimetype';
5+
6+
describe('Mimetype', () => {
7+
8+
it('is rule', () => {
9+
assert.instanceOf(new Mimetype('foo'), AbstractRule);
10+
});
11+
12+
it('values is valid', () => {
13+
assert.isTrue(new Mimetype('application/javascript').validate('js'));
14+
assert.isTrue(new Mimetype('application/json').validate('json'));
15+
assert.isTrue(new Mimetype('text/plain').validate('txt'));
16+
});
17+
18+
it('values is not valid', () => {
19+
assert.isFalse(new Mimetype('foo').validate('bar'));
20+
assert.isFalse(new Mimetype('txt').validate('foo/txt'));
21+
});
22+
23+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ describe('Validator', () => {
8080
assert.instanceOf(V.max(undefined), V);
8181
assert.instanceOf(V.max(100), V);
8282
assert.instanceOf(V.max(100, false), V);
83+
assert.instanceOf(V.mimetype('foo'), V);
8384
assert.instanceOf(V.min(), V);
8485
assert.instanceOf(V.min(null), V);
8586
assert.instanceOf(V.min(undefined), V);
@@ -240,6 +241,7 @@ describe('Validator', () => {
240241
assert.instanceOf(v.max(), V);
241242
assert.instanceOf(v.max(100), V);
242243
assert.instanceOf(v.max(100, false), V);
244+
assert.instanceOf(v.mimetype('foo'), V);
243245
assert.instanceOf(v.min(), V);
244246
assert.instanceOf(v.min(100), V);
245247
assert.instanceOf(v.min(100, false), V);

0 commit comments

Comments
 (0)