misskey-awawa/src/api/it.ts

519 lines
13 KiB
TypeScript
Raw Normal View History

2017-03-02 17:42:17 +00:00
/**
* it
*
*/
2017-03-02 12:54:46 +00:00
import * as mongo from 'mongodb';
import hasDuplicates from '../common/has-duplicates';
type Validator<T> = (value: T) => boolean | Error;
interface Factory {
2017-03-02 17:10:27 +00:00
/**
2017-03-02 18:03:14 +00:00
* qedはQ.E.D.'QueryEnD'
2017-03-02 17:10:27 +00:00
*/
qed: () => [any, Error];
2017-03-02 12:54:46 +00:00
required: () => Factory;
2017-03-02 17:42:17 +00:00
default: (value: any) => Factory;
2017-03-02 12:54:46 +00:00
validate: (validator: Validator<any>) => Factory;
}
class FactoryCore implements Factory {
value: any;
error: Error;
constructor() {
this.value = null;
this.error = null;
}
/**
* undefined  null
*/
required() {
if (this.error === null && this.value === null) {
this.error = new Error('required');
}
return this;
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: any) {
if (this.value === null) {
this.value = value;
}
return this;
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [any, Error] {
2017-03-02 12:54:46 +00:00
return [this.value, this.error];
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<any>) {
if (this.error || this.value === null) return this;
const result = validator(this.value);
if (result === false) {
this.error = new Error('invalid-format');
} else if (result instanceof Error) {
this.error = result;
}
return this;
}
}
class BooleanFactory extends FactoryCore {
value: boolean;
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (typeof value != 'boolean') {
this.error = new Error('must-be-a-boolean');
} else {
this.value = value;
}
}
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: boolean) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [boolean, Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<boolean>) {
return super.validate(validator);
}
}
class NumberFactory extends FactoryCore {
value: number;
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (!Number.isFinite(value)) {
this.error = new Error('must-be-a-number');
} else {
this.value = value;
}
}
/**
*
* @param min
* @param max
*/
range(min: number, max: number) {
if (this.error || this.value === null) return this;
if (this.value < min || this.value > max) {
this.error = new Error('invalid-range');
}
return this;
}
2017-03-02 17:42:17 +00:00
/**
*
* @param value
*/
min(value: number) {
if (this.error || this.value === null) return this;
if (this.value < value) {
this.error = new Error('invalid-range');
}
return this;
}
/**
*
* @param value
*/
max(value: number) {
if (this.error || this.value === null) return this;
if (this.value > value) {
this.error = new Error('invalid-range');
}
return this;
}
2017-03-02 12:54:46 +00:00
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: number) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [number, Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<number>) {
return super.validate(validator);
}
}
class StringFactory extends FactoryCore {
value: string;
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (typeof value != 'string') {
this.error = new Error('must-be-a-string');
} else {
this.value = value;
}
}
/**
*
* @param min
* @param max
*/
range(min: number, max: number) {
if (this.error || this.value === null) return this;
if (this.value.length < min || this.value.length > max) {
this.error = new Error('invalid-range');
}
return this;
}
trim() {
if (this.error || this.value === null) return this;
this.value = this.value.trim();
return this;
}
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: string) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [string, Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<string>) {
return super.validate(validator);
}
}
class ArrayFactory extends FactoryCore {
value: any[];
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (!Array.isArray(value)) {
this.error = new Error('must-be-an-array');
} else {
this.value = value;
}
}
/**
* (=)
*/
unique() {
if (this.error || this.value === null) return this;
if (hasDuplicates(this.value)) {
this.error = new Error('must-be-unique');
}
return this;
}
/**
*
* @param min
* @param max
*/
range(min: number, max: number) {
if (this.error || this.value === null) return this;
if (this.value.length < min || this.value.length > max) {
this.error = new Error('invalid-range');
}
return this;
}
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: any[]) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [any[], Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<any[]>) {
return super.validate(validator);
}
}
class IdFactory extends FactoryCore {
value: mongo.ObjectID;
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) {
this.error = new Error('must-be-an-id');
} else {
this.value = new mongo.ObjectID(value);
}
}
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: mongo.ObjectID) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [any[], Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<any[]>) {
return super.validate(validator);
}
}
class ObjectFactory extends FactoryCore {
value: any;
error: Error;
constructor(value) {
super();
if (value === undefined || value === null) {
this.value = null;
} else if (typeof value != 'object') {
this.error = new Error('must-be-an-object');
} else {
this.value = value;
}
}
/**
* undefined  null
*/
required() {
return super.required();
}
2017-03-02 17:42:17 +00:00
/**
*
*/
default(value: any) {
return super.default(value);
}
2017-03-02 12:54:46 +00:00
/**
*
*/
2017-03-02 17:10:27 +00:00
qed(): [any, Error] {
return super.qed();
2017-03-02 12:54:46 +00:00
}
/**
*
* false
* @param validator
*/
validate(validator: Validator<any>) {
return super.validate(validator);
}
}
2017-03-02 17:10:27 +00:00
type It = {
2017-03-02 12:54:46 +00:00
must: {
be: {
a: {
string: () => StringFactory;
number: () => NumberFactory;
boolean: () => BooleanFactory;
};
an: {
id: () => IdFactory;
array: () => ArrayFactory;
object: () => ObjectFactory;
};
};
};
2017-03-02 17:10:27 +00:00
expect: {
string: () => StringFactory;
number: () => NumberFactory;
boolean: () => BooleanFactory;
id: () => IdFactory;
array: () => ArrayFactory;
object: () => ObjectFactory;
};
2017-03-02 12:54:46 +00:00
};
2017-03-02 17:10:27 +00:00
const it = (value: any) => ({
2017-03-02 12:54:46 +00:00
must: {
be: {
a: {
string: () => new StringFactory(value),
number: () => new NumberFactory(value),
boolean: () => new BooleanFactory(value)
},
an: {
id: () => new IdFactory(value),
array: () => new ArrayFactory(value),
object: () => new ObjectFactory(value)
}
}
2017-03-02 17:10:27 +00:00
},
expect: {
string: () => new StringFactory(value),
number: () => new NumberFactory(value),
boolean: () => new BooleanFactory(value),
id: () => new IdFactory(value),
array: () => new ArrayFactory(value),
object: () => new ObjectFactory(value)
2017-03-02 12:54:46 +00:00
}
});
type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object';
2017-03-02 17:10:27 +00:00
function x(value: any): It;
function x(value: any, type: 'id', isRequired?: boolean, validator?: Validator<mongo.ObjectID> | Validator<mongo.ObjectID>[]): [mongo.ObjectID, Error];
function x(value: any, type: 'string', isRequired?: boolean, validator?: Validator<string> | Validator<string>[]): [string, Error];
function x(value: any, type: 'number', isRequired?: boolean, validator?: Validator<number> | Validator<number>[]): [number, Error];
function x(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error];
function x(value: any, type: 'array', isRequired?: boolean, validator?: Validator<any[]> | Validator<any[]>[]): [any[], Error];
function x(value: any, type: 'set', isRequired?: boolean, validator?: Validator<any[]> | Validator<any[]>[]): [any[], Error];
function x(value: any, type: 'object', isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): [any, Error];
function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator<any> | Validator<any>[]): any {
if (typeof type === 'undefined') return it(value);
2017-03-02 12:54:46 +00:00
2017-03-02 17:10:27 +00:00
let factory: Factory = null;
2017-03-02 12:54:46 +00:00
2017-03-02 17:10:27 +00:00
switch (type) {
case 'id': factory = it(value).expect.id(); break;
case 'string': factory = it(value).expect.string(); break;
case 'number': factory = it(value).expect.number(); break;
case 'boolean': factory = it(value).expect.boolean(); break;
case 'array': factory = it(value).expect.array(); break;
case 'set': factory = it(value).expect.array().unique(); break;
case 'object': factory = it(value).expect.object(); break;
2017-03-02 12:54:46 +00:00
}
2017-03-02 17:10:27 +00:00
if (isRequired) factory = factory.required();
2017-03-02 12:54:46 +00:00
2017-03-02 17:10:27 +00:00
if (validator) {
(Array.isArray(validator) ? validator : [validator])
.forEach(v => factory = factory.validate(v));
2017-03-02 12:54:46 +00:00
}
2017-03-02 17:10:27 +00:00
return factory;
2017-03-02 12:54:46 +00:00
}
2017-03-02 17:10:27 +00:00
export default x;