shelving 1.121.1 → 1.122.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/schema/FileSchema.d.ts +14 -0
- package/schema/FileSchema.js +25 -0
- package/util/file.d.ts +8 -0
- package/util/file.js +13 -0
- package/util/index.d.ts +1 -0
- package/util/index.js +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FileTypes } from "../util/file.js";
|
|
2
|
+
import { StringSchema, type StringSchemaOptions } from "./StringSchema.js";
|
|
3
|
+
/** Allowed options for `FileSchema` */
|
|
4
|
+
export type FileSchemaOptions = StringSchemaOptions & {
|
|
5
|
+
readonly formats: FileTypes;
|
|
6
|
+
};
|
|
7
|
+
/** Validate a file name matching one or more extensions. */
|
|
8
|
+
export declare class FileSchema extends StringSchema {
|
|
9
|
+
readonly formats: FileTypes;
|
|
10
|
+
constructor(options: {
|
|
11
|
+
formats: FileTypes;
|
|
12
|
+
} & StringSchemaOptions);
|
|
13
|
+
validate(unsafeValue?: unknown): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Feedback } from "../feedback/Feedback.js";
|
|
2
|
+
import { getOptionalFileExtension } from "../util/file.js";
|
|
3
|
+
import { isProp } from "../util/object.js";
|
|
4
|
+
import { getValidationContext } from "../util/validate.js";
|
|
5
|
+
import { StringSchema } from "./StringSchema.js";
|
|
6
|
+
/** Validate a file name matching one or more extensions. */
|
|
7
|
+
export class FileSchema extends StringSchema {
|
|
8
|
+
formats;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super(options);
|
|
11
|
+
this.formats = options.formats;
|
|
12
|
+
}
|
|
13
|
+
validate(unsafeValue = this.value) {
|
|
14
|
+
const path = super.validate(unsafeValue);
|
|
15
|
+
const { action } = getValidationContext();
|
|
16
|
+
if (action === "set" || action === "update") {
|
|
17
|
+
const extension = getOptionalFileExtension(path);
|
|
18
|
+
if (!extension)
|
|
19
|
+
throw new Feedback("Must be file name with extension", unsafeValue);
|
|
20
|
+
if (!isProp(this.formats, extension))
|
|
21
|
+
throw new Feedback("Invalid file type", extension);
|
|
22
|
+
}
|
|
23
|
+
return path;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/util/file.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** List of file types in `extension: mime` format. */
|
|
2
|
+
export type FileTypes = {
|
|
3
|
+
[extension: string]: string;
|
|
4
|
+
};
|
|
5
|
+
/** Get the file extension from a file path, or return `undefined` if the input has no extension. */
|
|
6
|
+
export declare function getOptionalFileExtension(file: string): string | undefined;
|
|
7
|
+
/** Get the file extension from a file path, or throw `ValueError` if the input has no extension. */
|
|
8
|
+
export declare function getFileExtension(path: string): string;
|
package/util/file.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ValueError } from "../error/ValueError.js";
|
|
2
|
+
/** Get the file extension from a file path, or return `undefined` if the input has no extension. */
|
|
3
|
+
export function getOptionalFileExtension(file) {
|
|
4
|
+
const i = file.lastIndexOf(".");
|
|
5
|
+
return (i >= 0 && file.slice(i + 1)) || undefined;
|
|
6
|
+
}
|
|
7
|
+
/** Get the file extension from a file path, or throw `ValueError` if the input has no extension. */
|
|
8
|
+
export function getFileExtension(path) {
|
|
9
|
+
const extension = getOptionalFileExtension(path);
|
|
10
|
+
if (!extension)
|
|
11
|
+
throw new ValueError("File extension is required", path);
|
|
12
|
+
return extension;
|
|
13
|
+
}
|
package/util/index.d.ts
CHANGED
package/util/index.js
CHANGED