本文整理匯總了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
};
}