首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在流星中使用mongo模式验证和类型记录?

如何在流星中使用mongo模式验证和类型记录?
EN

Stack Overflow用户
提问于 2016-05-24 07:27:03
回答 1查看 1.5K关注 0票数 4

在使用类型记录时,是否有可在Meteor 1.3中使用架构验证的包。Meteor指南中推荐的包(al契据:simple-schema)似乎没有定义文件。

那么用什么代替,或者打字本有一个内置的方式来做这件事呢?

EN

回答 1

Stack Overflow用户

发布于 2017-04-24 02:53:05

Meteor1.3及以上版本使用的最佳包是节点-简单模式

从医生那里:

SimpleSchema的历史 SimpleSchema于2013年年中作为Meteor软件包首次发布。1.0版于2014年9月发布。2016年年中,2.0版作为NPM软件包发布,可以在Meteor、NodeJS或静态浏览器应用程序中使用。 安装 npm安装简单模式还有其他名为simpleschema和simpleschema的NPM包。请确保安装正确的软件包。“辛普尔”里没有"e“。

因此,类型记录中的正确导入如下所示:

代码语言:javascript
复制
import SimpleSchema from 'simpl-schema';

但你的问题是关于打字的。Meteor键入的起点是https://github.com/meteor-typescript/meteor-typescript-libs/tree/master/definitions的Meteor类型库。

在其中,您可以找到collection2和简单模式的定义,但它是旧的简单模式。它们确实为您提供了一个良好的起点,您将希望稍后再来拿起collection2的。最后,经过大约一个星期的搜索/等等,我根据“流星Git”的原版写了我自己的集。希望他们能对未来的搜索者有所帮助,即使他们对你来说有点晚了。

代码语言:javascript
复制
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日的每一个版本的最新版本。

关于加分,下面是验证-方法的集合,接下来您将查找该集合:

代码语言:javascript
复制
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世界中开始模式和验证的搜索者,这里有几个模块将有助于完善您的验证包:

代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37406989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档