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

Commit 6e2755e

Browse files
feat(rules): Added Writable rule
1 parent 239ddb9 commit 6e2755e

9 files changed

Lines changed: 80 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,6 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
177177
- [VideoUrl](docs/video-url.md)
178178
- [Vowel](docs/vowel.md)
179179
- [When](docs/when.md)
180+
- [Writable](docs/writable.md)
180181
- [Xdigit](docs/xdigit.md)
181182
- [Yes](docs/yes.md)

awesome-validator.min.js

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

docs/writable.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Writable
2+
3+
Validates if the given input is writable file.
4+
5+
Valid values:
6+
7+
```js
8+
validator.writable().validate(__filename);
9+
```
10+
11+
Invalid values:
12+
13+
```js
14+
validator.writable().validate(__dirname);
15+
```

src/rules/abstract-file-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ export abstract class AbstractFileSystem extends AbstractRule {
4444
/**
4545
* Validate File System.
4646
*/
47-
protected abstract validateFileSystem(stas: Stats): boolean;
47+
protected abstract validateFileSystem(stats: Stats): boolean;
4848
}

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@ export * from './version';
9898
export * from './video-url';
9999
export * from './vowel';
100100
export * from './when';
101+
export * from './writable';
101102
export * from './xdigit';
102103
export * from './yes';

src/rules/writable.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as fs from 'fs';
2+
3+
import { AbstractRule } from './abstract-rule';
4+
5+
export class Writable extends AbstractRule {
6+
7+
/**
8+
* Validate.
9+
*/
10+
public validate(input: any): boolean {
11+
try {
12+
fs.accessSync(input, fs.constants.W_OK);
13+
14+
return true;
15+
} catch (e) {}
16+
17+
return false;
18+
}
19+
}

src/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export class Validator extends AllOf {
108108
public static videoUrl = (service?: VideoService): Validator => new Validator(new rules.VideoUrl(service));
109109
public static vowel = (additionalChars?: string): Validator => new Validator(new rules.Vowel(additionalChars));
110110
public static when = (whenRule: Validatable, thenRule: Validatable, elseRule?: Validatable): Validator => new Validator(new rules.When(whenRule, thenRule, elseRule));
111+
public static writable = (): Validator => new Validator(new rules.Writable());
111112
public static xdigit = (additionalChars?: string): Validator => new Validator(new rules.Xdigit(additionalChars));
112113
public static yes = (additionalChars?: string): Validator => new Validator(new rules.Yes(additionalChars));
113114

@@ -211,6 +212,7 @@ export class Validator extends AllOf {
211212
public videoUrl = (service?: VideoService): this => this.addRule(new rules.VideoUrl(service));
212213
public vowel = (additionalChars?: string): this => this.addRule(new rules.Vowel(additionalChars));
213214
public when = (whenRule: Validatable, thenRule: Validatable, elseRule?: Validatable): this => this.addRule(new rules.When(whenRule, thenRule, elseRule));
215+
public writable = (): this => this.addRule(new rules.Writable());
214216
public xdigit = (additionalChars?: string): this => this.addRule(new rules.Xdigit(additionalChars));
215217
public yes = (additionalChars?: string): this => this.addRule(new rules.Yes(additionalChars));
216218
/* tslint:enable completed-docs max-line-length */

test/rules/writable.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as mock from 'mock-fs';
2+
3+
import { assert } from 'chai';
4+
5+
import { AbstractRule } from '../../src/rules/abstract-rule';
6+
import { Writable } from '../../src/rules/writable';
7+
8+
describe('Writable', () => {
9+
10+
let writable: Writable;
11+
12+
beforeEach(() => {
13+
writable = new Writable();
14+
15+
mock({
16+
'file.txt': 'foo'
17+
});
18+
});
19+
20+
afterEach(() => {
21+
mock.restore();
22+
});
23+
24+
it('is rule', () => {
25+
assert.instanceOf(writable, AbstractRule);
26+
});
27+
28+
it('values is valid', () => {
29+
assert.isTrue(writable.validate(''));
30+
assert.isTrue(writable.validate('file.txt'));
31+
});
32+
33+
it('values is not valid', () => {
34+
assert.isFalse(writable.validate('foo'));
35+
assert.isFalse(writable.validate('file-not-found.txt'));
36+
});
37+
38+
});

test/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ describe('Validator', () => {
164164
assert.instanceOf(V.vowel('foo'), V);
165165
assert.instanceOf(V.when(V.alwaysValid(), V.alwaysValid()), V);
166166
assert.instanceOf(V.when(V.alwaysInvalid(), V.alwaysValid(), V.alwaysValid()), V);
167+
assert.instanceOf(V.writable(), V);
167168
assert.instanceOf(V.xdigit(), V);
168169
assert.instanceOf(V.xdigit('foo'), V);
169170
assert.instanceOf(V.yes(), V);
@@ -317,6 +318,7 @@ describe('Validator', () => {
317318
assert.instanceOf(v.vowel('foo'), V);
318319
assert.instanceOf(v.when(V.alwaysValid(), V.alwaysValid()), V);
319320
assert.instanceOf(v.when(V.alwaysInvalid(), V.alwaysValid(), V.alwaysValid()), V);
321+
assert.instanceOf(v.writable(), V);
320322
assert.instanceOf(v.xdigit(), V);
321323
assert.instanceOf(v.xdigit('foo'), V);
322324
assert.instanceOf(v.yes(), V);

0 commit comments

Comments
 (0)