iceshrimp-161sh/packages/backend/src/misc/cafy-id.ts
syuilo 14148f6c4a refactoring
Resolve #7779
2021-11-12 02:02:25 +09:00

33 lines
659 B
TypeScript

import { Context } from 'cafy';
export class ID<Maybe = string> extends Context<string | (Maybe extends {} ? string : Maybe)> {
public readonly name = 'ID';
constructor(optional = false, nullable = false) {
super(optional, nullable);
this.push((v: any) => {
if (typeof v !== 'string') {
return new Error('must-be-an-id');
}
return true;
});
}
public getType() {
return super.getType('String');
}
public makeOptional(): ID<undefined> {
return new ID(true, false);
}
public makeNullable(): ID<null> {
return new ID(false, true);
}
public makeOptionalNullable(): ID<undefined | null> {
return new ID(true, true);
}
}