Basic version of Simpl-Schema definition

This commit is contained in:
arichter83
2018-12-04 21:02:19 +01:00
parent 40dac65f4e
commit d01679afa6
4 changed files with 315 additions and 0 deletions
+195
View File
@@ -0,0 +1,195 @@
// Type definitions for simpl-schema 0.1
// Project: https://github.com/aldeed/simple-schema-js
// Definitions by: Andreas Richter <https://github.com/arichter83>
// Qkramer <https://github.com/Qkramer>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
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 CustomValidationContext {
value: any;
}
interface SchemaDefinition {
type: any;
label?: string | (() => string);
optional?: boolean | (() => boolean);
min?: number | boolean | Date | (() => number | boolean | Date);
max?: number | boolean | Date | (() => number | boolean | Date);
minCount?: number | (() => number);
maxCount?: number | (() => number);
allowedValues?: any[] | (() => any[]);
decimal?: boolean;
exclusiveMax?: boolean;
exclusiveMin?: boolean;
regEx?: RegExp | RegExp[];
custom?: () => any;
blackbox?: boolean;
autoValue?: () => any;
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[]): SimpleSchema;
namedContext(name?: string): SimpleSchemaValidationContextStatic;
addValidator(validator: () => boolean): 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): () => boolean;
RegEx: {
Email: RegExp;
EmailWithTLD: RegExp;
Domain: RegExp;
WeakDomain: RegExp;
IP: RegExp;
IPv4: RegExp;
IPv6: RegExp;
Url: RegExp;
Id: RegExp;
ZipCode: RegExp;
Phone: RegExp;
};
ErrorTypes: {
REQUIRED: string,
MIN_STRING: string,
MAX_STRING: string,
MIN_NUMBER: string,
MAX_NUMBER: string,
MIN_NUMBER_EXCLUSIVE: string,
MAX_NUMBER_EXCLUSIVE: string,
MIN_DATE: string,
MAX_DATE: string,
BAD_DATE: string,
MIN_COUNT: string,
MAX_COUNT: string,
MUST_BE_INTEGER: string,
VALUE_NOT_ALLOWED: string,
EXPECTED_TYPE: string,
FAILED_REGULAR_EXPRESSION: string,
KEY_NOT_IN_SCHEMA: string
};
}
interface ValidationOption {
modifier?: boolean;
upsert?: boolean;
clean?: boolean;
filter?: boolean;
upsertextendedCustomContext?: boolean;
}
interface SimpleSchemaValidationContextStaticKeys {
name: string;
type: string;
value?: any;
}
interface SimpleSchemaError {
name: string;
type: string;
}
interface SimpleSchemaValidationContextStatic {
validate(obj: any, options?: ValidationOption): boolean;
validateOne(doc: any, keyName: string, options?: ValidationOption): boolean;
resetValidation(): void;
isValid(): boolean;
invalidKeys(): SimpleSchemaValidationContextStaticKeys[];
addInvalidKeys(errors: SimpleSchemaError[]): void;
keyIsInvalid(name: any): boolean;
keyErrorMessage(name: any): string;
getErrorObject(): any;
}
interface MongoObjectStatic {
forEachNode(func: (() => void), 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: (() => boolean)): 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;
validate(obj: any, options?: ValidationOption): void;
addValidator(validator: () => boolean): 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;
+96
View File
@@ -0,0 +1,96 @@
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
},
createdAt: {
type: Date,
autoValue: () => new Date(),
},
title: {
type: String,
label: "Argument",
/* TODO, this does not work: "The containing arrow function captures the global value of 'this' which implicitly has type 'any'."
custom: () => {
const 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,
}
}
*/
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }