如何在流星中使用mingo模式验证与打字稿?

时间:2016-05-24 07:27:03

标签: node.js mongodb meteor typescript database-schema

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

那么使用什么,或者也许是Typescript有内置的方法来做到这一点?

1 个答案:

答案 0 :(得分:4)

用于Meteor 1.3以及使用Typescript的最佳软件包是aldeed:node-simple-schema

来自文档:

  

SimpleSchema的历史

     

SimpleSchema于2013年中期首次作为Meteor软件包发布。   版本1.0于2014年9月发布。在2016年中期,版本2.0   作为NPM包发布,可用于Meteor,NodeJS,   或静态浏览器应用程序。

     

<强>安装

     

npm install simpl-schema还有其他名为NPM的软件包   simpleschema和simple-schema。确保安装正确   包。 “简化”上没有“e”。

因此,Typescript中的正确导入如下所示:

import SimpleSchema from 'simpl-schema';

但你的问题是关于打字的问题。 Meteor打字的起点是https://github.com/meteor-typescript/meteor-typescript-libs/tree/master/definitions的Meteor打字图书馆 在其中,您将找到collection2和simple-schema的定义,但它是简单模式。它们确实为你提供了一个很好的起点,你会想要回来并稍微拿起那些2个。最终,经过大约一周的搜索/等。我根据Meteor Git上的原始编写了我自己的一套。希望他们对未来的搜索者有所帮助,即使他们对你来说有点迟了。

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