gpt4 book ai didi

node.js - 如何在 meteor 中使用 mongo 模式验证和 typescript ?

转载 作者:可可西里 更新时间:2023-11-01 09:12:59 26 4
gpt4 key购买 nike

在使用 Typescript 时,是否有一个包可以在 Meteor 1.3 中使用模式验证。 Meteor 指南中推荐的包 (aldeed:simple-schema) 似乎没有定义文件。

那么应该改用什么,或者 Typescript 有内置的方法来做到这一点?

最佳答案

用于 Meteor 1.3 和 Typescript 的最佳软件包是 aldeed:node-simple-schema .

来自文档:

The History of SimpleSchema

SimpleSchema was first released as a Meteor package in mid-2013. Version 1.0 was released in September 2014. In mid-2016, version 2.0 was released as an NPM package, which can be used in Meteor, NodeJS, or static browser apps.

Installation

npm install simpl-schema There are other NPM packages named simpleschema and simple-schema. Make sure you install the right package. There is no "e" on "simpl".

所以在 Typescript 中正确的导入看起来像这样:

import SimpleSchema from 'simpl-schema';

但是您的问题专门针对打字。 Meteor 类型的起点是位于 https://github.com/meteor-typescript/meteor-typescript-libs/tree/master/definitions 的 Meteor 类型库。 .
在其中,您会找到 collection2 和 simple-schema 的定义,但它是 simple-schema。它们确实为您提供了一个很好的起点,过一会儿您会想回来挑选收藏品。最终,经过大约一周的搜索等。我根据 Meteor Git 上的原始版本编写了自己的集合。希望它们对 future 的搜索者有所帮助,即使它们对您来说有点晚了。

declare module "simpl-schema" {

export class ValidationContext {
constructor(ss: any);
addValidationErrors(errors: any): void;
clean(...args: any[]): any;
getErrorForKey(key: any, ...args: any[]): any;
isValid(): any;
keyErrorMessage(key: any, ...args: any[]): any;
keyIsInvalid(key: any, ...args: any[]): any;
reset(): void;
setValidationErrors(errors: any): void;
validate(obj: any, ...args: any[]): any;
validationErrors(): any;
}

interface SchemaDefinition {
type: any;
label?: string | Function;
optional?: boolean | Function;
min?: number | boolean | Date | Function;
max?: number | boolean | Date | Function;
minCount?: number | Function;
maxCount?: number | Function;
allowedValues?: any[] | Function;
decimal?: boolean;
exclusiveMax?: boolean;
exclusiveMin?: boolean;
regEx?: RegExp | RegExp[];
custom?: Function;
blackbox?: boolean;
autoValue?: Function;
defaultValue?: any;
trim?: boolean;
}

interface CleanOption {
filter?: boolean;
autoConvert?: boolean;
removeEmptyStrings?: boolean;
trimStrings?: boolean;
getAutoValues?: boolean;
isModifier?: boolean;
extendAutoValueContext?: boolean;
}

interface SimpleSchemaStatic {
new(schema: {[key: string]: SchemaDefinition} | any[]): SimpleSchemaStatic;
debug: boolean;
namedContext(name?: string): SimpleSchemaValidationContextStatic;
addValidator(validator: Function): any;
pick(...fields: string[]): SimpleSchemaStatic;
omit(...fields: string[]): SimpleSchemaStatic;
clean(doc: any, options?: CleanOption): any;
schema(key?: string): SchemaDefinition | SchemaDefinition[];
getDefinition(key: string, propList?: any, functionContext?: any): any;
keyIsInBlackBox(key: string): boolean;
labels(labels: {[key: string]: string}): void;
label(key: any): any;
Integer: RegExp;
messages(messages: any): void;
messageForError(type: any, key: any, def: any, value: any): string;
allowsKey(key: any): string;
newContext(): SimpleSchemaValidationContextStatic;
objectKeys(keyPrefix: any): any[];
validate(obj: any, options?: ValidationOption): void;
validator(options: ValidationOption): Function;
RegEx: {
Email: RegExp;
EmailWithTLD: RegExp;
Domain: RegExp;
WeakDomain: RegExp;
IP: RegExp;
IPv4: RegExp;
IPv6: RegExp;
Url: RegExp;
Id: RegExp;
ZipCode: RegExp;
Phone: RegExp;
};
}

interface ValidationOption {
modifier?: boolean;
upsert?: boolean;
clean?: boolean;
filter?: boolean;
upsertextendedCustomContext?: boolean;
}

interface SimpleSchemaValidationContextStatic {
validate(obj: any, options?: ValidationOption): boolean;
validateOne(doc: any, keyName: string, options?: ValidationOption): boolean;
resetValidation(): void;
isValid(): boolean;
invalidKeys(): { name: string; type: string; value?: any; }[];
addInvalidKeys(errors: { name: string, type: string; }[]): void;
keyIsInvalid(name: any): boolean;
keyErrorMessage(name: any): string;
getErrorObject(): any;
}

interface MongoObjectStatic {
forEachNode(func: Function, options?: {endPointsOnly: boolean;}): void;
getValueForPosition(position: string): any;
setValueForPosition(position: string, value: any): void;
removeValueForPosition(position: string): void;
getKeyForPosition(position: string): any;
getGenericKeyForPosition(position: string): any;
getInfoForKey(key: string): any;
getPositionForKey(key: string): string;
getPositionsForGenericKey(key: string): string[];
getValueForKey(key: string): any;
addKey(key: string, val: any, op: string): any;
removeGenericKeys(keys: string[]): void;
removeGenericKey(key: string): void;
removeKey(key: string): void;
removeKeys(keys: string[]): void;
filterGenericKeys(test: Function): void;
setValueForKey(key: string, val: any): void;
setValueForGenericKey(key: string, val: any): void;
getObject(): any;
getFlatObject(options?: {keepArrays?: boolean}): any;
affectsKey(key: string): any;
affectsGenericKey(key: string): any;
affectsGenericKeyImplicit(key: string): any;
}

export const SimpleSchema: SimpleSchemaStatic;
export const SimpleSchemaValidationContext: SimpleSchemaValidationContextStatic;
export const MongoObject: MongoObjectStatic;

export interface SimpleSchema {
debug: boolean;
addValidator(validator: Function): any;
extendOptions(options: {[key: string]: any}): void;
messages(messages: any): void;
RegEx: {
Email: RegExp;
Domain: RegExp;
WeakDomain: RegExp;
IP: RegExp;
IPv4: RegExp;
IPv6: RegExp;
Url: RegExp;
Id: RegExp;
ZipCode: RegExp;
Phone: RegExp;
};
}

export interface MongoObject {
expandKey(val: any, key: string, obj: any): void;
}

export default SimpleSchema;
}

这是基于截至 2017 年 4 月 23 日的最新版本。

对于奖励积分,这是验证方法的集合,您接下来将寻找它:

declare module "meteor/mdg:validated-method" {
declare class ValidatedMethod<T> extends MeteorValidatedMethod.ValidatedMethod<T> { }

declare module MeteorValidatedMethod {
export class ValidatedMethod<T> {
constructor(options: ValidatedMethodOptions<T>);
call(options?: T, cb?: (err, res)=> void): void;
}

interface ValidatedMethodOptions<T> {
name: string;
mixins?: Function[];
validate: any;
applyOptions: any;
run(opts: T);
}
}
}

对于在 Meteor 世界中着手进行模式和验证的搜索者,这里还有一些模块将有助于完善您的验证包:

aldeed:collection2-core       2.0.0  Core package for aldeed:collection2
aldeed:schema-deny 2.0.0 Deny inserting or updating certain properties through schema options
aldeed:schema-index 2.0.0 Control some MongoDB indexing with schema options
mdg:validated-method 1.1.0 A simple wrapper for Meteor.methods
mdg:validation-error 0.5.1 A standard validation error to be used by form/method/validation packages

关于node.js - 如何在 meteor 中使用 mongo 模式验证和 typescript ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37406989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com