diff --git a/types/simpl-schema/index.d.ts b/types/simpl-schema/index.d.ts new file mode 100644 index 0000000000..c90d2ba93b --- /dev/null +++ b/types/simpl-schema/index.d.ts @@ -0,0 +1,158 @@ +// from https://github.com/aldeed/simple-schema-js/issues/90 + +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; + 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): any; + 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; +} diff --git a/types/simpl-schema/simpl-schema-tests.ts b/types/simpl-schema/simpl-schema-tests.ts new file mode 100644 index 0000000000..6081a5a11b --- /dev/null +++ b/types/simpl-schema/simpl-schema-tests.ts @@ -0,0 +1,90 @@ +import SimpleSchema from 'simpl-schema'; + +const StringSchema = new SimpleSchema({ + basicString: { + type: String + }, + limitedString: { + type: String, + allowedValues: ['pro', 'con'] + }, + regExpString: { + type: String, + regEx: SimpleSchema.RegEx.Id + }, + title: { + type: String, + label: "Argument", + custom: function () { + var text = this.value; + if(text.length > 10) return { type: SimpleSchema.ErrorTypes.MAX_STRING, max: 10 }; + } + }, +}); + +StringSchema.validate({ + basicString: "Test", + limitedString: "pro", + regExpString: "id" +}); + + /* + , + title: { + type: String, + label: "Argument", + custom: function () { + var text = this.value; + text = text.replace(/https?:\/\/(www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6})\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi, '$2'); + + if(text.length > MAX_ARGUMENT_LENGTH) return { type: SimpleSchema.ErrorTypes.MAX_STRING, max: MAX_ARGUMENT_LENGTH } + else if(text.length < MIN_ARGUMENT_LENGTH) return { type: SimpleSchema.ErrorTypes.MIN_STRING, min: MIN_ARGUMENT_LENGTH } + } + }, + questionId: { + type: String, + regEx: SimpleSchema.RegEx.Id + }, + side: { + type: String, + allowedValues: ['pro', 'con'] + }, + userId: { + type: String, + regEx: SimpleSchema.RegEx.Id + }, + createdAt: { + type: Date, + autoValue: () => new Date(), + }, + clapCount: { + type: SimpleSchema.Integer, + defaultValue: 0 + }, + highlight: { + type: Array, + optional: true + }, + 'highlight.$': { + type: Object + }, + 'highlight.$.start': SimpleSchema.Integer, + 'highlight.$.end': SimpleSchema.Integer, + 'highlight.$.text': { + type: String, + trim: false + } + */ + /* + , + { + clean: { + filter: true, + autoConvert: true, + removeEmptyStrings: true, + trimStrings: true, + getAutoValues: true, + removeNullsFromArrays: true, + } + } + */ diff --git a/types/simpl-schema/tsconfig.json b/types/simpl-schema/tsconfig.json new file mode 100644 index 0000000000..d079496f4e --- /dev/null +++ b/types/simpl-schema/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "simpl-schema-tests.ts" + ] +} diff --git a/types/simpl-schema/tslint.json b/types/simpl-schema/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/simpl-schema/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }