We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Writable
1 parent 239ddb9 commit 6e2755eCopy full SHA for 6e2755e
9 files changed
README.md
@@ -177,5 +177,6 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
177
- [VideoUrl](docs/video-url.md)
178
- [Vowel](docs/vowel.md)
179
- [When](docs/when.md)
180
+- [Writable](docs/writable.md)
181
- [Xdigit](docs/xdigit.md)
182
- [Yes](docs/yes.md)
awesome-validator.min.js
docs/writable.md
@@ -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
14
+validator.writable().validate(__dirname);
15
src/rules/abstract-file-system.ts
@@ -44,5 +44,5 @@ export abstract class AbstractFileSystem extends AbstractRule {
44
/**
45
* Validate File System.
46
*/
47
- protected abstract validateFileSystem(stas: Stats): boolean;
+ protected abstract validateFileSystem(stats: Stats): boolean;
48
}
src/rules/index.ts
@@ -98,5 +98,6 @@ export * from './version';
98
export * from './video-url';
99
export * from './vowel';
100
export * from './when';
101
+export * from './writable';
102
export * from './xdigit';
103
export * from './yes';
src/rules/writable.ts
@@ -0,0 +1,19 @@
+import * as fs from 'fs';
+import { AbstractRule } from './abstract-rule';
+export class Writable extends AbstractRule {
+ /**
+ * Validate.
+ */
+ public validate(input: any): boolean {
+ try {
+ fs.accessSync(input, fs.constants.W_OK);
+ return true;
+ } catch (e) {}
16
17
+ return false;
18
+ }
19
+}
src/validator.ts
@@ -108,6 +108,7 @@ export class Validator extends AllOf {
108
public static videoUrl = (service?: VideoService): Validator => new Validator(new rules.VideoUrl(service));
109
public static vowel = (additionalChars?: string): Validator => new Validator(new rules.Vowel(additionalChars));
110
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());
112
public static xdigit = (additionalChars?: string): Validator => new Validator(new rules.Xdigit(additionalChars));
113
public static yes = (additionalChars?: string): Validator => new Validator(new rules.Yes(additionalChars));
114
@@ -211,6 +212,7 @@ export class Validator extends AllOf {
211
212
public videoUrl = (service?: VideoService): this => this.addRule(new rules.VideoUrl(service));
213
public vowel = (additionalChars?: string): this => this.addRule(new rules.Vowel(additionalChars));
214
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());
216
public xdigit = (additionalChars?: string): this => this.addRule(new rules.Xdigit(additionalChars));
217
public yes = (additionalChars?: string): this => this.addRule(new rules.Yes(additionalChars));
218
/* tslint:enable completed-docs max-line-length */
test/rules/writable.ts
@@ -0,0 +1,38 @@
+import * as mock from 'mock-fs';
+import { assert } from 'chai';
+import { AbstractRule } from '../../src/rules/abstract-rule';
+import { Writable } from '../../src/rules/writable';
+describe('Writable', () => {
+ let writable: Writable;
+ beforeEach(() => {
+ writable = new Writable();
+ mock({
+ 'file.txt': 'foo'
+ });
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
@@ -164,6 +164,7 @@ describe('Validator', () => {
164
assert.instanceOf(V.vowel('foo'), V);
165
assert.instanceOf(V.when(V.alwaysValid(), V.alwaysValid()), V);
166
assert.instanceOf(V.when(V.alwaysInvalid(), V.alwaysValid(), V.alwaysValid()), V);
167
+ assert.instanceOf(V.writable(), V);
168
assert.instanceOf(V.xdigit(), V);
169
assert.instanceOf(V.xdigit('foo'), V);
170
assert.instanceOf(V.yes(), V);
@@ -317,6 +318,7 @@ describe('Validator', () => {
317
318
assert.instanceOf(v.vowel('foo'), V);
319
assert.instanceOf(v.when(V.alwaysValid(), V.alwaysValid()), V);
320
assert.instanceOf(v.when(V.alwaysInvalid(), V.alwaysValid(), V.alwaysValid()), V);
321
+ assert.instanceOf(v.writable(), V);
322
assert.instanceOf(v.xdigit(), V);
323
assert.instanceOf(v.xdigit('foo'), V);
324
assert.instanceOf(v.yes(), V);
0 commit comments