From 45df9cc2da7de4cb945e905b9ae134ba95af756d Mon Sep 17 00:00:00 2001 From: Bart van der Schoor Date: Sun, 20 Apr 2014 01:01:38 +0200 Subject: [PATCH] added definitions for joi --- README.md | 1 + joi/joi-tests.ts | 403 +++++++++++++++++++++++++++++++++++++++++++++++ joi/joi.d.ts | 377 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 781 insertions(+) create mode 100644 joi/joi-tests.ts create mode 100644 joi/joi.d.ts diff --git a/README.md b/README.md index 4120a354ef..c97da76e75 100755 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ List of Definitions * [jake](https://github.com/mde/jake) (by [Kon](http://phyzkit.net/)) * [Jasmine](http://pivotal.github.com/jasmine/) (by [Boris Yankov](https://github.com/borisyankov)) * [Jasmine-jQuery](https://github.com/velesin/jasmine-jquery) (by [Gregor Stamac](https://github.com/gstamac)) +* [Joi](https://github.com/spumko/joi) (by [Bart van der Schoor](https://github.com/Bartvds)) * [JointJS](http://www.jointjs.com/) (by [Aidan Reel](http://github.com/areel)) * [jQRangeSlider](http://ghusse.github.com/jQRangeSlider) (by [Dániel Tar](https://github.com/qcz)) * [jQuery](http://jquery.com/) (from TypeScript samples) diff --git a/joi/joi-tests.ts b/joi/joi-tests.ts new file mode 100644 index 0000000000..45a46dcd06 --- /dev/null +++ b/joi/joi-tests.ts @@ -0,0 +1,403 @@ +/// +/// + +import Joi = require('joi'); + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +var x: any = null; +var value: any = null; +var num: number = 0; +var str: string = ''; +var bool: boolean = false; +var exp: RegExp = null; +var obj: Object = null; +var date: Date = null; +var bin: NodeBuffer = null; +var err: Error = null; +var func: Function = null; + +var anyArr: any[] = []; +var numArr: number[] = []; +var strArr: string[] = []; +var boolArr: boolean[] = []; +var expArr: RegExp[] = []; +var objArr: Object[] = []; +var bufArr: NodeBuffer[] = []; +var errArr: Error[] = []; +var funcArr: Function[] = []; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +var validOpts: Joi.ValidationOptions = null; + +validOpts = {abortEarly: bool}; +validOpts = {convert: bool}; +validOpts = {allowUnknown: bool}; +validOpts = {skipFunctions: bool}; +validOpts = {stripUnknown: bool}; +validOpts = {language: bool}; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +var renOpts: Joi.RenameOptions = null; + +renOpts = {alias: bool}; +renOpts = {multiple: bool}; +renOpts = {override: bool}; + +var validErr: Joi.ValidationError = null; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +var schema: Joi.Schema = null; + +var anySchema: Joi.AnySchema = null; +var numSchema: Joi.NumberSchema = null; +var strSchema: Joi.StringSchema = null; +var arrSchema: Joi.ArraySchema = null; +var boolSchema: Joi.BooleanSchema = null; +var binSchema: Joi.BinarySchema = null; +var dateSchema: Joi.DateSchema = null; +var funcSchema: Joi.FunctionSchema = null; +var objSchema: Joi.ObjectSchema = null; + +var schemaArr: Joi.Schema[] = []; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +schema = anySchema; +schema = numSchema; +schema = strSchema; +schema = arrSchema; +schema = boolSchema; +schema = binSchema; +schema = dateSchema; +schema = funcSchema; +schema = objSchema; + +anySchema = anySchema; +anySchema = numSchema; +anySchema = strSchema; +anySchema = arrSchema; +anySchema = boolSchema; +anySchema = binSchema; +anySchema = dateSchema; +anySchema = funcSchema; +anySchema = objSchema; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +var schemaMap: Joi.SchemaMap = null; + +schemaMap = { + a: numSchema, + b: strSchema +}; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +anySchema = Joi.any(); + +anySchema.validate(x, (err: Joi.ValidationError, value: any) => { + +}); + +module common { + anySchema = anySchema.allow(x); + anySchema = anySchema.valid(x); + anySchema = anySchema.invalid(x); + anySchema = anySchema.default(x); + + anySchema = anySchema.required(); + anySchema = anySchema.optional(); + + anySchema = anySchema.description(str); + anySchema = anySchema.notes(str); + anySchema = anySchema.notes(strArr); + anySchema = anySchema.tags(str); + anySchema = anySchema.tags(strArr); + + anySchema = anySchema.options(validOpts); + anySchema = anySchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +arrSchema = Joi.array(); + +arrSchema = arrSchema.min(num); +arrSchema = arrSchema.max(num); +arrSchema = arrSchema.length(num); + +arrSchema = arrSchema.includes(numSchema); +arrSchema = arrSchema.includes(numSchema, strSchema); +arrSchema = arrSchema.includes([numSchema, strSchema]); + +arrSchema = arrSchema.excludes(numSchema); +arrSchema = arrSchema.excludes(numSchema, strSchema); +arrSchema = arrSchema.excludes([numSchema, strSchema]); + +// - - - - - - - - + +module common { + arrSchema = arrSchema.allow(anyArr); + arrSchema = arrSchema.valid(anyArr); + arrSchema = arrSchema.invalid(anyArr); + arrSchema = arrSchema.default(anyArr); + + arrSchema = arrSchema.required(); + arrSchema = arrSchema.optional(); + + arrSchema = arrSchema.description(str); + arrSchema = arrSchema.notes(str); + arrSchema = arrSchema.notes(strArr); + arrSchema = arrSchema.tags(str); + arrSchema = arrSchema.tags(strArr); + + arrSchema = arrSchema.options(validOpts); + arrSchema = arrSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +boolSchema = Joi.bool(); +boolSchema = Joi.boolean(); + +module common { + boolSchema = boolSchema.allow(bool); + boolSchema = boolSchema.valid(bool); + boolSchema = boolSchema.invalid(bool); + boolSchema = boolSchema.default(bool); + + boolSchema = boolSchema.required(); + boolSchema = boolSchema.optional(); + + boolSchema = boolSchema.description(str); + boolSchema = boolSchema.notes(str); + boolSchema = boolSchema.notes(strArr); + boolSchema = boolSchema.tags(str); + boolSchema = boolSchema.tags(strArr); + + boolSchema = boolSchema.options(validOpts); + boolSchema = boolSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +binSchema = Joi.binary(); + +binSchema = binSchema.min(num); +binSchema = binSchema.max(num); +binSchema = binSchema.length(num); + +module common { + binSchema = binSchema.allow(bin); + binSchema = binSchema.valid(bin); + binSchema = binSchema.invalid(bin); + binSchema = binSchema.default(bin); + + binSchema = binSchema.required(); + binSchema = binSchema.optional(); + + binSchema = binSchema.description(str); + binSchema = binSchema.notes(str); + binSchema = binSchema.notes(strArr); + binSchema = binSchema.tags(str); + binSchema = binSchema.tags(strArr); + + binSchema = binSchema.options(validOpts); + binSchema = binSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +dateSchema = Joi.date(); + +dateSchema = dateSchema.min(date); +dateSchema = dateSchema.max(date); + +dateSchema = dateSchema.min(str); +dateSchema = dateSchema.max(str); + +dateSchema = dateSchema.min(num); +dateSchema = dateSchema.max(num); + +module common { + dateSchema = dateSchema.allow(date); + dateSchema = dateSchema.valid(date); + dateSchema = dateSchema.invalid(date); + dateSchema = dateSchema.default(date); + + dateSchema = dateSchema.allow(num); + dateSchema = dateSchema.valid(num); + dateSchema = dateSchema.invalid(num); + dateSchema = dateSchema.default(num); + + dateSchema = dateSchema.allow(str); + dateSchema = dateSchema.valid(str); + dateSchema = dateSchema.invalid(str); + dateSchema = dateSchema.default(str); + + dateSchema = dateSchema.required(); + dateSchema = dateSchema.optional(); + + dateSchema = dateSchema.description(str); + dateSchema = dateSchema.notes(str); + dateSchema = dateSchema.notes(strArr); + dateSchema = dateSchema.tags(str); + dateSchema = dateSchema.tags(strArr); + + dateSchema = dateSchema.options(validOpts); + dateSchema = dateSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +funcSchema = Joi.func(); + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +numSchema = Joi.number(); + +numSchema = numSchema.min(num); +numSchema = numSchema.max(num); +numSchema = numSchema.integer(); + +module common { + numSchema = numSchema.allow(num); + numSchema = numSchema.valid(num); + numSchema = numSchema.invalid(num); + numSchema = numSchema.default(num); + + numSchema = numSchema.required(); + numSchema = numSchema.optional(); + + numSchema = numSchema.description(str); + numSchema = numSchema.notes(str); + numSchema = numSchema.notes(strArr); + numSchema = numSchema.tags(str); + numSchema = numSchema.tags(strArr); + + numSchema = numSchema.options(validOpts); + numSchema = numSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +objSchema = Joi.object(); +objSchema = Joi.object(schemaMap); + +objSchema = objSchema.keys(); +objSchema = objSchema.keys(schemaMap); + +objSchema = objSchema.min(num); +objSchema = objSchema.max(num); +objSchema = objSchema.length(num); + +objSchema = objSchema.with(str, str); +objSchema = objSchema.with(str, strArr); + +objSchema = objSchema.without(str, str); +objSchema = objSchema.without(str, strArr); + +objSchema = objSchema.xor(str, str, str); +objSchema = objSchema.xor(strArr); + +objSchema = objSchema.or(str, str, str); +objSchema = objSchema.or(strArr); + +objSchema = objSchema.rename(str, str); +objSchema = objSchema.rename(str, str, renOpts); + +module common { + objSchema = objSchema.allow(obj); + objSchema = objSchema.valid(obj); + objSchema = objSchema.invalid(obj); + objSchema = objSchema.default(obj); + + objSchema = objSchema.required(); + objSchema = objSchema.optional(); + + objSchema = objSchema.description(str); + objSchema = objSchema.notes(str); + objSchema = objSchema.notes(strArr); + objSchema = objSchema.tags(str); + objSchema = objSchema.tags(strArr); + + objSchema = objSchema.options(validOpts); + objSchema = objSchema.strict(); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +strSchema = Joi.string(); + +strSchema = strSchema.insensitive(); +strSchema = strSchema.min(num); +strSchema = strSchema.max(num); +strSchema = strSchema.length(num); +strSchema = strSchema.regex(exp); +strSchema = strSchema.alphanum(); +strSchema = strSchema.token(); +strSchema = strSchema.email(); +strSchema = strSchema.guid(); +strSchema = strSchema.isoDate(); + +module common { + strSchema = strSchema.allow(x); + strSchema = strSchema.allow(x, x); + strSchema = strSchema.allow(anyArr); + + strSchema = strSchema.valid(x); + strSchema = strSchema.valid(x, x); + strSchema = strSchema.valid(anyArr); + + strSchema = strSchema.invalid(x); + strSchema = strSchema.invalid(x, x); + strSchema = strSchema.invalid(anyArr); + + strSchema = strSchema.required(); + + strSchema = strSchema.optional(); + + strSchema = strSchema.description(str); + + strSchema = strSchema.notes(str); + strSchema = strSchema.notes(strArr); + + strSchema = strSchema.tags(str); + strSchema = strSchema.tags(strArr); + + strSchema = strSchema.options(validOpts); + strSchema = strSchema.strict(); + strSchema = strSchema.default(x); +} + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +schema = Joi.alternatives(schemaArr); +schema = Joi.alternatives(schema, anySchema, boolSchema); + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +Joi.validate(value, schema); +Joi.validate(value, schema, validOpts); +Joi.validate(value, schema, validOpts, (err, value) => { + x = value; + str = err.message; + str = err.details[0].path; + str = err.details[0].message; + str = err.details[0].type; +}); +// variant +Joi.validate(num, schema, validOpts, (err, value) => { + num = value; +}); + +// plain opts +Joi.validate(value, {}); + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + +schema = Joi.compile(obj); diff --git a/joi/joi.d.ts b/joi/joi.d.ts new file mode 100644 index 0000000000..344738e891 --- /dev/null +++ b/joi/joi.d.ts @@ -0,0 +1,377 @@ +// Type definitions for joi v3.1.0 +// Project: https://github.com/spumko/joi +// Definitions by: Bart van der Schoor +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'joi' { + + export interface ValidationOptions { + // when true, stops validation on the first error, otherwise returns all the errors found. Defaults to true. + abortEarly?: boolean; + // when true, attempts to cast values to the required types (e.g. a string to a number). Defaults to true. + convert?: boolean; + // when true, allows object to contain unknown keys which are ignored. Defaults to false. + allowUnknown?: boolean; + // when true, ignores unknown keys with a function value. Defaults to false. + skipFunctions?: boolean; + // when true, unknown keys are deleted (only when value is an object). Defaults to false. + stripUnknown?: boolean; + // overrides individual error messages. Defaults to no override ({}). + language?: Object + } + + export interface RenameOptions { + // if true, does not delete the old key name, keeping both the new and old keys in place. Defaults to false. + alias?: boolean; + // if true, allows renaming multiple keys to the same destination where the last rename wins. Defaults to false. + multiple?: boolean; + // if true, allows renaming a key over an existing key. Defaults to false. + override?: boolean; + } + + export interface ValidationError { + message: string; + details: ValidationErrorItem[]; + simple (): string; + annotated (): string; + } + + export interface ValidationErrorItem { + message: string; + type: string; + path: string; + options?: ValidationOptions; + } + + export interface SchemaMap { + [key: string]: Schema; + } + + export interface Schema extends AnySchema { + } + + export interface AnySchema> { + + validate(value: U, options?: ValidationOptions, callback?: (err: ValidationError, value: U) => void): void; + + /** + * Whitelists a value + */ + allow(value: any, ...values : any[]): T; + allow(values: any[]): T; + + /** + * Adds the provided values into the allowed whitelist and marks them as the only valid values allowed. + */ + valid(value: any, ...values : any[]): T; + valid(values: any[]): T; + + /** + * Blacklists a value + */ + invalid(value: any, ...values : any[]): T; + invalid(values: any[]): T; + + /** + * Marks a key as required which will not allow undefined as value. All keys are optional by default. + */ + required(): T; + + /** + * Marks a key as optional which will allow undefined as values. Used to annotate the schema for readability as all keys are optional by default. + */ + optional(): T; + + /** + * Annotates the key + */ + description(desc: string): T; + + /** + * Annotates the key + */ + notes(notes: string): T; + notes(notes: string[]): T; + + /** + * Annotates the key + */ + tags(notes: string): T; + tags(notes: string[]): T; + + /** + * Overrides the global validate() options for the current key and any sub-key + */ + options(options: ValidationOptions): T; + + /** + * Sets the options.convert options to false which prevent type casting for the current key and any child keys. + */ + strict(): T; + + /** + * Sets a default value if the original value is undefined + */ + default(value: any): T; + } + + export interface BooleanSchema extends AnySchema { + + } + + export interface NumberSchema extends AnySchema { + /** + * Specifies the minimum value. + */ + min(limit: number): NumberSchema; + + /** + * Specifies the maximum value. + */ + max(limit: number): NumberSchema; + + /** + * Requires the number to be an integer (no floating point). + */ + integer(): NumberSchema; + } + + export interface StringSchema extends AnySchema { + /** + * Allows the value to match any whitelist of blacklist item in a case insensitive comparison. + */ + insensitive(): StringSchema; + + /** + * Specifies the minimum number string characters. + */ + min(limit: number): StringSchema; + + /** + * Specifies the maximum number of string characters. + */ + max(limit: number): StringSchema; + + /** + * Specifies the exact string length required + */ + length(limit: number): StringSchema; + + /** + * Defines a regular expression rule. + */ + regex(pattern: RegExp): StringSchema; + + /** + * Requires the string value to only contain a-z, A-Z, and 0-9. + */ + alphanum(): StringSchema; + + /** + * Requires the string value to only contain a-z, A-Z, 0-9, and underscore _. + */ + token(): StringSchema; + + /** + * Requires the string value to be a valid email address. + */ + email(): StringSchema; + + /** + * Requires the string value to be a valid GUID. + */ + guid(): StringSchema; + + /** + * Requires the string value to be in valid ISO 8601 date format. + */ + isoDate(): StringSchema; + + } + + export interface ArraySchema extends AnySchema { + /** + * List the types allowed for the array value + */ + includes(type: Schema, ...types: Schema[]): ArraySchema; + includes(types: Schema[]): ArraySchema; + + /** + * List the types forbidden for the array values. + */ + excludes(type: Schema, ...types: Schema[]): ArraySchema; + excludes(types: Schema[]): ArraySchema; + + /** + * Specifies the minimum number of items in the array. + */ + min(limit: number): ArraySchema; + + /** + * Specifies the maximum number of items in the array. + */ + max(limit: number): ArraySchema; + + /** + * Specifies the exact number of items in the array. + */ + length(limit: number): ArraySchema; + + } + + export interface ObjectSchema extends AnySchema { + /** + * Sets the allowed object keys. + */ + keys(schema?: SchemaMap): ObjectSchema; + + /** + * Specifies the minimum number of keys in the object. + */ + min(limit: number): ObjectSchema; + + /** + * Specifies the maximum number of keys in the object. + */ + max(limit: number): ObjectSchema; + + /** + * Specifies the exact number of keys in the object. + */ + length(limit: number): ObjectSchema; + + /** + * Requires the presence of other keys whenever the specified key is present. + */ + with(key: string, peers: string): ObjectSchema; + with(key: string, peers: string[]): ObjectSchema; + + /** + * Forbids the presence of other keys whenever the specified is present. + */ + without(key: string, peers: string): ObjectSchema; + without(key: string, peers: string[]): ObjectSchema; + + /** + * Defines an exclusive relationship between a set of keys. one of them is required but not at the same time where: + */ + xor(peer1: string, peer2: string, ...peers: string[]): ObjectSchema; + xor(peers: string[]): ObjectSchema; + + /** + * Defines a relationship between keys where one of the peers is required (and more than one is allowed). + */ + or(peer1: string, peer2: string, ...peers: string[]): ObjectSchema; + or(peers: string[]): ObjectSchema; + + /** + * Renames a key to another name (deletes the renamed key). + */ + rename(from: string, to: string, options?: RenameOptions): ObjectSchema; + } + + export interface BinarySchema extends AnySchema { + /** + * Specifies the minimum length of the buffer. + */ + min(limit: number): BinarySchema; + + /** + * Specifies the maximum length of the buffer. + */ + max(limit: number): BinarySchema; + + /** + * Specifies the exact length of the buffer: + */ + length(limit: number): BinarySchema; + } + + export interface DateSchema extends AnySchema { + + /** + * Specifies the oldest date allowed. + */ + min(date: Date): DateSchema; + min(date: number): DateSchema; + min(date: string): DateSchema; + + /** + * Specifies the latest date allowed. + */ + max(date: Date): DateSchema; + max(date: number): DateSchema; + max(date: string): DateSchema; + } + + export interface FunctionSchema extends AnySchema { + + } + + // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + + /** + * Generates a schema object that matches any data type. + */ + export function any(): Schema; + + /** + * Generates a schema object that matches an array data type. + */ + export function array(): ArraySchema; + + /** + * Generates a schema object that matches a boolean data type (as well as the strings 'true', 'false', 'yes', and 'no'). Can also be called via bool(). + */ + export function bool(): BooleanSchema; + + export function boolean(): BooleanSchema; + + /** + * Generates a schema object that matches a Buffer data type (as well as the strings which will be converted to Buffers). + */ + export function binary(): BinarySchema; + + /** + * Generates a schema object that matches a date type (as well as a JavaScript date string or number of milliseconds). + */ + export function date(): DateSchema; + + /** + * Generates a schema object that matches a function type. + */ + export function func(): FunctionSchema; + + /** + * Generates a schema object that matches a number data type (as well as strings that can be converted to numbers). + */ + export function number(): NumberSchema; + + /** + * Generates a schema object that matches an object data type (as well as JSON strings that parsed into objects). + */ + export function object(schema?: SchemaMap): ObjectSchema; + + /** + * Generates a schema object that matches a string data type. Note that empty strings are not allowed by default and must be enabled with allow(''). + */ + export function string(): StringSchema; + + /** + * Generates a type that will match one of the provided alternative schemas + */ + export function alternatives(types: Schema[]): Schema; + export function alternatives(type1: Schema, type2: Schema, ...types: Schema[]): Schema; + + /** + * Validates a value using the given schema and options. + */ + export function validate(value: T, schema: Schema, options?: ValidationOptions, callback?: (err: ValidationError, value: T) => void): void; + export function validate(value: T, schema: Object, options?: ValidationOptions, callback?: (err: ValidationError, value: T) => void): void; + + /** + * Converts literal schema definition to joi schema object (or returns the same back if already a joi schema object). + */ + export function compile(schema: Object): Schema; + +}