array type

This commit is contained in:
2020-05-09 21:30:37 +02:00
parent 469afeb777
commit 7a13de1d03
11 changed files with 138 additions and 56 deletions

View File

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

View File

@ -1,34 +1,45 @@
export class TypeValidation {
private readonly _general_type: string;
import { OptionType } from '../OptionType';
public get general_type (): string {
return this._general_type;
export class TypeValidation {
private readonly _option_type: string;
public get option_type (): OptionType {
return this._option_type as OptionType;
}
public get string_type (): 'string'|'number'|'boolean'|'array' {
return this._general_type as 'string'|'number'|'boolean'|'array';
public get persistent_type (): 'string'|'number'|'boolean'|'array' {
return this.option_type as 'string'|'number'|'boolean'|'array';
}
public get string_type (): 'string'|'number'|'boolean'|'object' {
const type = this.option_type;
if (type === 'array')
return 'object';
return type as 'string'|'number'|'boolean';
}
public constructor (type: string) {
this._general_type = type;
this._option_type = type;
}
public validate_type (value: unknown): boolean {
return typeof value === this.general_type;
const type_match = typeof value === this.string_type;
const array_match = this.option_type !== 'array' || Array.isArray (value);
return type_match && array_match;
}
public to_type (value: unknown): Promise<unknown> {
if (this.general_type === 'string')
if (this.option_type === 'string')
return Promise.resolve (String (value));
if (this.general_type === 'number') {
if (this.option_type === 'number') {
const as_num = parseInt (String (value));
if (isNaN (as_num))
throw new Error ('value is not a number');
return Promise.resolve (as_num);
}
if (this.general_type === 'boolean') {
if (this.option_type === 'boolean') {
const as_num = parseInt (String (value));
if (
as_num !== 1 && as_num !== 0
@ -39,6 +50,16 @@ export class TypeValidation {
as_num === 1 || (/true/iu).test (String (value))
);
}
if (this.option_type === 'array') {
if (typeof value === 'string') {
return Promise.resolve (value.split (',')
.map ((v) => v.trim ()));
}
if (this.validate_type (value))
return Promise.resolve (value);
throw new Error ('value is not an array');
}
throw new Error ('unknown type');
}
}