本文整理汇总了TypeScript中phaethon.validate类的典型用法代码示例。如果您正苦于以下问题:TypeScript validate类的具体用法?TypeScript validate怎么用?TypeScript validate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了validate类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: login
export async function login(request: ServerRequest, session: Session): Promise<LoginResult> {
const username = validate.expect(
request.query["username"], validate.isString);
const password = validate.expect(
request.query["password"], validate.isString);
const passwordHash = getPasswordHash(username, password);
const results = await find(users, { username, passwordHash });
if (results.length === 0) {
return { ok: false, message: "Username or password incorrect" };
}
const user = results[0];
session.userId = user._id;
return { ok: true };
}
示例2: remove
export async function remove(request: ServerRequest, session: Session) {
const user = await getUserOrError(session);
const id = validate.expect(
request.query["id"], validate.isString);
await database.remove(database.notes,
{ _id: new ObjectID(id), userId: user._id });
return {};
}
示例3: update
export async function update(request: ServerRequest, session: Session): Promise<ItemResult> {
const user = await getUserOrError(session);
const id = validate.expect(
request.query["id"], validate.isString);
const content = validate.expect(
request.query["content"], validate.isString);
const note: Note = {
_id: new ObjectID(id),
userId: user._id,
content
};
await database.update(database.notes,
{ _id: new ObjectID(id), userId: user._id }, note);
return {
id: note._id.toHexString(),
content: note.content
};
}
示例4: insert
export async function insert(request: ServerRequest, session: Session): Promise<ItemResult> {
const user = await getUserOrError(session);
const content = validate.expect(
request.query["content"], validate.isString);
const note: Note = {
_id: undefined,
userId: user._id,
content
};
await database.insert(database.notes, note);
return {
id: note._id.toHexString(),
content: note.content
};
}
示例5: find
export async function find(request: ServerRequest, session: Session): Promise<ItemResult> {
const user = await getUserOrError(session);
const id = validate.expect(
request.query["id"], validate.isString);
const notes = await database.find(database.notes,
{ _id: new ObjectID(id), userId: user._id });
if (notes.length === 0) {
throw new ServerError(StatusCode.ClientErrorNotFound);
}
const note = notes[0];
return {
id: note._id.toHexString(),
content: note.content
};
}