本文整理汇总了TypeScript中cafy.str.range方法的典型用法代码示例。如果您正苦于以下问题:TypeScript str.range方法的具体用法?TypeScript str.range怎么用?TypeScript str.range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cafy.str
的用法示例。
在下文中一共展示了str.range方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
export default async (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'title' parameter
const [title, titleErr] = $.str.range(1, 100).get(params.title);
if (titleErr) return rej('invalid title param');
// insert
const userList = await UserList.insert({
createdAt: new Date(),
userId: user._id,
title: title,
userIds: []
});
// Response
res(await pack(userList));
});
示例2: define
import $ from 'cafy';
import { define, AuthScopes, ApiErrorSources } from '../../modules/endpoint';
import { ObjectIdContext } from '../../modules/cafyValidators';
import { PostingResponseObject } from '../../modules/apiResponse/responseObjects';
import { ChatPostingDocument } from '../../modules/documents';
export default define({
params: {
text: $.str.range(1, 256).notMatch(/^\s*$/),
attachmentIds: $.array($.type(ObjectIdContext)).optional,
},
scopes: [AuthScopes.postingWrite]
}, async (manager) => {
const account = manager.authInfo!.user;
// params
const text: string = manager.params.text;
const attachmentIds: string[] | undefined = manager.params.attachmentIds;
let chatPostingDoc: ChatPostingDocument;
try {
chatPostingDoc = await manager.postingService.createChatPosting(account._id, text, attachmentIds);
}
catch (err) {
console.error(err);
manager.error(ApiErrorSources.serverError, { message: 'failed to create an chat posting' });
return;
}
await chatPostingDoc.populate(manager.db);
示例3: define
import $ from 'cafy';
import { define, AuthScopes, ApiErrorSources } from '../../../modules/endpoint';
import { CredentialValidationResponseObject } from '../../../modules/apiResponse/responseObjects';
export default define({
params: {
screenName: $.str.range(4, 15).match(/^[a-zA-Z0-9_-]+$/),
password: $.str.min(8)
},
scopes: [AuthScopes.authHost]
}, async (manager) => {
// params
const screenName: string = manager.params.screenName;
const password: string = manager.params.password;
const userDoc = await manager.userService.findByScreenName(screenName);
if (!userDoc) {
manager.ok(new CredentialValidationResponseObject({
isValid: false
}));
return;
}
const isValid = userDoc.validatePassword(password);
if (!isValid) {
manager.ok(new CredentialValidationResponseObject({
isValid: false
}));
return;
}
示例4: isAvailableScope
import $ from 'cafy';
import { define, AuthScopes, ApiErrorSources } from '../../modules/endpoint';
import { AppResponseObject } from '../../modules/apiResponse/responseObjects';
import { AppDocument } from '../../modules/documents';
function isAvailableScope(scopeId: string): boolean {
const scope = AuthScopes.toArray().find(s => s.id == scopeId);
return scope != null && scope.grantable;
}
export default define({
params: {
name: $.str.range(1, 32),
description: $.str.optional.max(256),
scopes: $.array($.str.pipe(scope => isAvailableScope(scope))).optional.unique()
},
scopes: [AuthScopes.appCreate]
}, async (manager) => {
const account = manager.authInfo!.user;
// params
const name: string = manager.params.name;
const description: string = manager.params.description || '';
const scopes: string[] = manager.params.scopes || [];
// if app name is duplicated
const tempApp = await manager.db.find('api.apps', { name: name });
if (tempApp) {
manager.error(ApiErrorSources.duplicatedAppName);