misskey-awawa/src/api/it.ts

566 lines
15 KiB
TypeScript
Raw Normal View History

2017-03-02 17:42:17 +00:00
/**
* it
*
*/
2017-03-02 21:11:11 +00:00
/**
* Usage Examples
*
* const [val, err] = it(x).must.be.a.string().or('asc desc').default('desc').qed();
* xは文字列でなければならず'asc''desc''desc'
*
* const [val, err] = it(x).must.be.a.number().required().range(0, 100).qed();
* xは数値でなければならず0~100
*
* const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed();
* xは配列でなければならず'strawberry pasta'
*
* ~~
* const [val, err] = it(x).must.be.a.string().required().qed();
*
* const [val, err] = it(x, 'string', true);
*
*
* ~BDD風記法~
* must.be.a(n)  expect :
* const [val, err] = it(x).expect.string().required().qed();
*/
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;
2017-03-02 20:33:47 +00:00
interface Query {
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
2017-03-02 20:33:47 +00:00
required: () => Query;
2017-03-02 12:54:46 +00:00
2017-03-02 20:33:47 +00:00
default: (value: any) => Query;
2017-03-02 17:42:17 +00:00
2017-03-02 20:33:47 +00:00
validate: (validator: Validator<any>) => Query;
2017-03-02 12:54:46 +00:00
}
2017-03-02 20:33:47 +00:00
class QueryCore implements Query {
2017-03-02 12:54:46 +00:00
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;
}
}
2017-03-02 20:33:47 +00:00
class BooleanQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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);
}
}
2017-03-02 20:33:47 +00:00
class NumberQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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);
}
}
2017-03-02 20:33:47 +00:00
class StringQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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);
}
2017-03-02 20:52:12 +00:00
/**
*
*
* @param pattern
*/
or(pattern: string | string[]) {
if (this.error || this.value === null) return this;
if (typeof pattern == 'string') pattern = pattern.split(' ');
const match = pattern.some(x => x === this.value);
if (!match) this.error = new Error('not-match-pattern');
return this;
}
/**
*
*
* @param pattern
*/
match(pattern: RegExp) {
if (this.error || this.value === null) return this;
if (!pattern.test(this.value)) this.error = new Error('not-match-pattern');
return this;
}
2017-03-02 12:54:46 +00:00
}
2017-03-02 20:33:47 +00:00
class ArrayQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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);
}
}
2017-03-02 20:33:47 +00:00
class IdQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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);
}
}
2017-03-02 20:33:47 +00:00
class ObjectQuery extends QueryCore {
2017-03-02 12:54:46 +00:00
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: {
2017-03-02 20:33:47 +00:00
string: () => StringQuery;
number: () => NumberQuery;
boolean: () => BooleanQuery;
2017-03-02 12:54:46 +00:00
};
an: {
2017-03-02 20:33:47 +00:00
id: () => IdQuery;
array: () => ArrayQuery;
object: () => ObjectQuery;
2017-03-02 12:54:46 +00:00
};
};
};
2017-03-02 17:10:27 +00:00
expect: {
2017-03-02 20:33:47 +00:00
string: () => StringQuery;
number: () => NumberQuery;
boolean: () => BooleanQuery;
id: () => IdQuery;
array: () => ArrayQuery;
object: () => ObjectQuery;
2017-03-02 17:10:27 +00:00
};
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: {
2017-03-02 20:33:47 +00:00
string: () => new StringQuery(value),
number: () => new NumberQuery(value),
boolean: () => new BooleanQuery(value)
2017-03-02 12:54:46 +00:00
},
an: {
2017-03-02 20:33:47 +00:00
id: () => new IdQuery(value),
array: () => new ArrayQuery(value),
object: () => new ObjectQuery(value)
2017-03-02 12:54:46 +00:00
}
}
2017-03-02 17:10:27 +00:00
},
expect: {
2017-03-02 20:33:47 +00:00
string: () => new StringQuery(value),
number: () => new NumberQuery(value),
boolean: () => new BooleanQuery(value),
id: () => new IdQuery(value),
array: () => new ArrayQuery(value),
object: () => new ObjectQuery(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 20:33:47 +00:00
let q: Query = null;
2017-03-02 12:54:46 +00:00
2017-03-02 17:10:27 +00:00
switch (type) {
2017-03-02 20:33:47 +00:00
case 'id': q = it(value).expect.id(); break;
case 'string': q = it(value).expect.string(); break;
case 'number': q = it(value).expect.number(); break;
case 'boolean': q = it(value).expect.boolean(); break;
case 'array': q = it(value).expect.array(); break;
case 'set': q = it(value).expect.array().unique(); break;
case 'object': q = it(value).expect.object(); break;
2017-03-02 12:54:46 +00:00
}
2017-03-02 20:33:47 +00:00
if (isRequired) q = q.required();
2017-03-02 12:54:46 +00:00
2017-03-02 17:10:27 +00:00
if (validator) {
(Array.isArray(validator) ? validator : [validator])
2017-03-02 20:33:47 +00:00
.forEach(v => q = q.validate(v));
2017-03-02 12:54:46 +00:00
}
2017-03-02 20:33:47 +00:00
return q;
2017-03-02 12:54:46 +00:00
}
2017-03-02 17:10:27 +00:00
export default x;