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

Commit 210429d

Browse files
feat(rules): Added Image rule
1 parent aa69678 commit 210429d

8 files changed

Lines changed: 73 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
106106
- [FunctionInstance](docs/function-instance.md)
107107
- [FunctionType](docs/function-type.md)
108108
- [Graph](docs/graph.md)
109+
- [Image](docs/image.md)
109110
- [Imei](docs/imei.md)
110111
- [In](docs/in.md)
111112
- [Infinite](docs/infinite.md)

awesome-validator.min.js

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

docs/image.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Image
2+
3+
Validates if the file is a valid image.
4+
5+
Valid values:
6+
7+
```js
8+
validator.image().validate('foo.png');
9+
validator.image().validate('foo.jpg');
10+
validator.image().validate('gif');
11+
```
12+
13+
Invalid values:
14+
15+
```js
16+
validator.image().validate('foo/png');
17+
validator.image().validate('foo/jpg');
18+
validator.image().validate('foobar');
19+
```

src/rules/image.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AbstractRule } from './abstract-rule';
2+
import { Mimetype } from './mimetype';
3+
4+
export class Image extends AbstractRule {
5+
6+
/**
7+
* Validate.
8+
*/
9+
public validate(input: any): boolean {
10+
return new Mimetype(/^image\//i).validate(input);
11+
}
12+
}

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * from './float-val';
2727
export * from './function-instance';
2828
export * from './function-type';
2929
export * from './graph';
30+
export * from './image';
3031
export * from './imei';
3132
export * from './in';
3233
export * from './infinite';

src/validator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export class Validator extends AllOf {
3737
public static functionInstance = (): Validator => new Validator(new rules.FunctionInstance());
3838
public static functionType = (): Validator => new Validator(new rules.FunctionType());
3939
public static graph = (additionalChars?: string): Validator => new Validator(new rules.Graph(additionalChars));
40+
public static image = (): Validator => new Validator(new rules.Image());
4041
public static imei = (): Validator => new Validator(new rules.Imei());
4142
public static in = (searcher?: any, contains: boolean = true, identical: boolean = false): Validator => new Validator(new rules.In(searcher, contains, identical));
4243
public static infinite = (): Validator => new Validator(new rules.Infinite());
@@ -52,7 +53,7 @@ export class Validator extends AllOf {
5253
public static luhn = (): Validator => new Validator(new rules.Luhn());
5354
public static macAddress = (): Validator => new Validator(new rules.MacAddress());
5455
public static max = (interval?: any, inclusive: boolean = true): Validator => new Validator(new rules.Max(interval, inclusive));
55-
public static mimetype = (mimetype: string | string[]): Validator => new Validator(new rules.Mimetype(mimetype));
56+
public static mimetype = (mimetype: any): Validator => new Validator(new rules.Mimetype(mimetype));
5657
public static min = (interval?: any, inclusive: boolean = true): Validator => new Validator(new rules.Min(interval, inclusive));
5758
public static multiple = (multipleOf: number): Validator => new Validator(new rules.Multiple(multipleOf));
5859
public static negative = (): Validator => new Validator(new rules.Negative());
@@ -146,6 +147,7 @@ export class Validator extends AllOf {
146147
public functionInstance = (): this => this.addRule(new rules.FunctionInstance());
147148
public functionType = (): this => this.addRule(new rules.FunctionType());
148149
public graph = (additionalChars?: string): this => this.addRule(new rules.Graph(additionalChars));
150+
public image = (): this => this.addRule(new rules.Image());
149151
public imei = (): this => this.addRule(new rules.Imei());
150152
public in = (searcher?: any, contains: boolean = true, identical: boolean = false): this => this.addRule(new rules.In(searcher, contains, identical));
151153
public infinite = (): this => this.addRule(new rules.Infinite());
@@ -161,7 +163,7 @@ export class Validator extends AllOf {
161163
public luhn = (): this => this.addRule(new rules.Luhn());
162164
public macAddress = (): this => this.addRule(new rules.MacAddress());
163165
public max = (interval?: any, inclusive: boolean = true): this => this.addRule(new rules.Max(interval, inclusive));
164-
public mimetype = (mimetype: string | string[]): this => this.addRule(new rules.Mimetype(mimetype));
166+
public mimetype = (mimetype: any): this => this.addRule(new rules.Mimetype(mimetype));
165167
public min = (interval?: any, inclusive: boolean = true): this => this.addRule(new rules.Min(interval, inclusive));
166168
public multiple = (multipleOf: number): this => this.addRule(new rules.Multiple(multipleOf));
167169
public negative = (): this => this.addRule(new rules.Negative());

test/rules/image.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { Image } from '../../src/rules/image';
5+
6+
describe('Image', () => {
7+
8+
let image: Image;
9+
10+
beforeEach(() => {
11+
image = new Image();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(image, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(image.validate('foo.png'));
20+
assert.isTrue(image.validate('foo.jpg'));
21+
assert.isTrue(image.validate('jpeg'));
22+
assert.isTrue(image.validate('gif'));
23+
});
24+
25+
it('values is not valid', () => {
26+
assert.isFalse(image.validate('foo/png'));
27+
assert.isFalse(image.validate('foo/jpg'));
28+
assert.isFalse(image.validate('foobar'));
29+
});
30+
31+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('Validator', () => {
5757
assert.instanceOf(V.functionType(), V);
5858
assert.instanceOf(V.graph(), V);
5959
assert.instanceOf(V.graph('foo'), V);
60+
assert.instanceOf(V.image(), V);
6061
assert.instanceOf(V.imei(), V);
6162
assert.instanceOf(V.in([]), V);
6263
assert.instanceOf(V.in([], false), V);
@@ -221,6 +222,7 @@ describe('Validator', () => {
221222
assert.instanceOf(v.functionType(), V);
222223
assert.instanceOf(v.graph(), V);
223224
assert.instanceOf(v.graph('foo'), V);
225+
assert.instanceOf(v.image(), V);
224226
assert.instanceOf(v.imei(), V);
225227
assert.instanceOf(v.in([]), V);
226228
assert.instanceOf(v.in([], false), V);

0 commit comments

Comments
 (0)