24 lines
710 B
TypeScript
24 lines
710 B
TypeScript
|
import fs from 'fs-extra';
|
||
|
import { TypeValidation } from './TypeValidation';
|
||
|
|
||
|
export class PathType extends TypeValidation {
|
||
|
public get string_type (): 'string'|'number'|'boolean'|'array' {
|
||
|
return 'string';
|
||
|
}
|
||
|
|
||
|
public async to_type (value: unknown): Promise<unknown> {
|
||
|
if (typeof value !== 'string')
|
||
|
throw new Error (`invalid type for ${this.general_type}`);
|
||
|
if (!await fs.pathExists (value))
|
||
|
throw new Error ('path does not exist');
|
||
|
if (this.general_type === 'path')
|
||
|
return value;
|
||
|
|
||
|
const stat = await fs.stat (value);
|
||
|
if (stat.isDirectory () === (this.general_type === 'folder'))
|
||
|
return value;
|
||
|
|
||
|
throw new Error ('cannot assign folder to file');
|
||
|
}
|
||
|
}
|