Merge pull request #29845 from SimonSchick/feat/joi-14.0

feat(joi): add v14.0
This commit is contained in:
Daniel Rosenwasser
2018-10-30 12:51:09 -07:00
committed by GitHub
8 changed files with 2530 additions and 56 deletions
+33 -50
View File
@@ -1,4 +1,4 @@
// Type definitions for joi 13.6
// Type definitions for joi 14.0
// Project: https://github.com/hapijs/joi
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
// Laurence Dougal Myers <https://github.com/laurence-myers>
@@ -53,7 +53,6 @@ export interface ValidationOptions {
* remove unknown elements from objects and arrays. Defaults to false
* - when true, all unknown elements will be removed
* - when an object:
* - arrays - set to true to remove unknown items from arrays.
* - objects - set to true to remove unknown keys from objects
*/
stripUnknown?: boolean | { arrays?: boolean; objects?: boolean };
@@ -257,8 +256,7 @@ export interface AnySchema extends JoiObject {
/**
* Validates a value using the schema and options.
*/
validate<T>(value: T): ValidationResult<T>;
validate<T>(value: T, options: ValidationOptions): ValidationResult<T>;
validate<T>(value: T, options?: ValidationOptions): ValidationResult<T>;
validate<T, R>(value: T, callback: (err: ValidationError, value: T) => R): R;
validate<T, R>(value: T, options: ValidationOptions, callback: (err: ValidationError, value: T) => R): R;
@@ -317,14 +315,12 @@ export interface AnySchema extends JoiObject {
/**
* Annotates the key
*/
notes(notes: string): this;
notes(notes: string[]): this;
notes(notes: string | string[]): this;
/**
* Annotates the key
*/
tags(notes: string): this;
tags(notes: string[]): this;
tags(notes: string | string[]): this;
/**
* Attaches metadata to the key.
@@ -369,8 +365,7 @@ export interface AnySchema extends JoiObject {
* Additionally, when specifying a method you must either have a description property on your method or the
* second parameter is required.
*/
default(value: any, description?: string): this;
default(): this;
default(value?: any, description?: string): this;
/**
* Returns a new type that is the result of adding the rules of one type to another.
@@ -380,8 +375,7 @@ export interface AnySchema extends JoiObject {
/**
* Converts the type into an alternatives type where the conditions are merged into the type definition where:
*/
when(ref: string, options: WhenOptions): AlternativesSchema;
when(ref: Reference, options: WhenOptions): AlternativesSchema;
when(ref: string | Reference, options: WhenOptions): AlternativesSchema;
when(ref: Schema, options: WhenSchemaOptions): AlternativesSchema;
/**
@@ -485,29 +479,25 @@ export interface NumberSchema extends AnySchema {
* Specifies the minimum value.
* It can also be a reference to another field.
*/
min(limit: number): this;
min(limit: Reference): this;
min(limit: number | Reference): this;
/**
* Specifies the maximum value.
* It can also be a reference to another field.
*/
max(limit: number): this;
max(limit: Reference): this;
max(limit: number | Reference): this;
/**
* Specifies that the value must be greater than limit.
* It can also be a reference to another field.
*/
greater(limit: number): this;
greater(limit: Reference): this;
greater(limit: number | Reference): this;
/**
* Specifies that the value must be less than limit.
* It can also be a reference to another field.
*/
less(limit: number): this;
less(limit: Reference): this;
less(limit: number | Reference): this;
/**
* Requires the number to be an integer (no floating point).
@@ -552,16 +542,14 @@ export interface StringSchema extends AnySchema {
* @param limit - the minimum number of string characters required. It can also be a reference to another field.
* @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
*/
min(limit: number, encoding?: string): this;
min(limit: Reference, encoding?: string): this;
min(limit: number | Reference, encoding?: string): this;
/**
* Specifies the maximum number of string characters.
* @param limit - the maximum number of string characters allowed. It can also be a reference to another field.
* @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
*/
max(limit: number, encoding?: string): this;
max(limit: Reference, encoding?: string): this;
max(limit: number | Reference, encoding?: string): this;
/**
* Specifies whether the string.max() limit should be used as a truncation.
@@ -591,8 +579,7 @@ export interface StringSchema extends AnySchema {
* @param limit - the required string length. It can also be a reference to another field.
* @param encoding - if specified, the string length is calculated in bytes using the provided encoding.
*/
length(limit: number, encoding?: string): this;
length(limit: Reference, encoding?: string): this;
length(limit: number | Reference, encoding?: string): this;
/**
* Defines a regular expression rule.
@@ -610,8 +597,7 @@ export interface StringSchema extends AnySchema {
* @param pattern - a regular expression object to match against, or a string of which all occurrences will be replaced.
* @param replacement - the string that will replace the pattern.
*/
replace(pattern: RegExp, replacement: string): this;
replace(pattern: string, replacement: string): this;
replace(pattern: RegExp | string, replacement: string): this;
/**
* Requires the string value to only contain a-z, A-Z, and 0-9.
@@ -739,8 +725,7 @@ export interface ArraySchema extends AnySchema {
/**
* Specifies the exact number of items in the array.
*/
length(limit: number): this;
length(limit: Reference): this;
length(limit: number | Reference): this;
/**
* Requires the array values to be unique.
@@ -816,14 +801,12 @@ export interface ObjectSchema extends AnySchema {
/**
* Requires the presence of other keys whenever the specified key is present.
*/
with(key: string, peers: string): this;
with(key: string, peers: string[]): this;
with(key: string, peers: string | string[]): this;
/**
* Forbids the presence of other keys whenever the specified is present.
*/
without(key: string, peers: string): this;
without(key: string, peers: string[]): this;
without(key: string, peers: string | string[]): this;
/**
* Renames a key to another name (deletes the renamed key).
@@ -833,8 +816,7 @@ export interface ObjectSchema extends AnySchema {
/**
* Verifies an assertion where.
*/
assert(ref: string, schema: SchemaLike, message?: string): this;
assert(ref: Reference, schema: SchemaLike, message?: string): this;
assert(ref: string | Reference, schema: SchemaLike, message?: string): this;
/**
* Overrides the handling of unknown keys for the scope of the current object only (does not apply to children).
@@ -847,6 +829,7 @@ export interface ObjectSchema extends AnySchema {
* @param constructor - the constructor function that the object must be an instance of.
* @param name - an alternate name to use in validation errors. This is useful when the constructor function does not have a name.
*/
// tslint:disable-next-line:ban-types
type(constructor: Function, name?: string): this;
/**
@@ -987,8 +970,7 @@ export interface FunctionSchema extends AnySchema {
export interface AlternativesSchema extends AnySchema {
try(types: SchemaLike[]): this;
try(...types: SchemaLike[]): this;
when(ref: string, options: WhenOptions): this;
when(ref: Reference, options: WhenOptions): this;
when(ref: string | Reference, options: WhenOptions): this;
when(ref: Schema, options: WhenSchemaOptions): this;
}
@@ -1037,6 +1019,13 @@ export interface Err extends JoiObject {
toString(): string;
}
export interface LazyOptions {
/**
* If true the schema generator will only be called once and the result will be cached.
*/
once?: boolean;
}
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
/**
@@ -1113,15 +1102,13 @@ export function alt(...types: SchemaLike[]): AlternativesSchema;
* Supports the same methods of the any() type.
* This is mostly useful for recursive schemas
*/
export function lazy(cb: () => Schema): LazySchema;
export function lazy(cb: () => Schema, options?: LazyOptions): LazySchema;
/**
* Validates a value using the given schema and options.
*/
export function validate<T>(value: T, schema: SchemaLike): ValidationResult<T>;
export function validate<T>(value: T, schema: SchemaLike, options?: ValidationOptions): ValidationResult<T>;
export function validate<T, R>(value: T, schema: SchemaLike, callback: (err: ValidationError, value: T) => R): R;
export function validate<T>(value: T, schema: SchemaLike, options: ValidationOptions): ValidationResult<T>;
export function validate<T, R>(value: T, schema: SchemaLike, options: ValidationOptions, callback: (err: ValidationError, value: T) => R): R;
/**
@@ -1161,8 +1148,7 @@ export function isRef(ref: any): ref is Reference;
* Get a sub-schema of an existing schema based on a `path` that can be either a string or an array
* of strings For string values path separator is a dot (`.`)
*/
export function reach(schema: ObjectSchema, path: string): Schema;
export function reach(schema: ObjectSchema, path: string[]): Schema;
export function reach(schema: ObjectSchema, path: string | string[]): Schema;
/**
* Creates a new Joi instance customized with the extension(s) you provide included.
@@ -1251,14 +1237,12 @@ export function description(desc: string): Schema;
/**
* Annotates the key
*/
export function notes(notes: string): Schema;
export function notes(notes: string[]): Schema;
export function notes(notes: string | string[]): Schema;
/**
* Annotates the key
*/
export function tags(notes: string): Schema;
export function tags(notes: string[]): Schema;
export function tags(notes: string | string[]): Schema;
/**
* Attaches metadata to the key.
@@ -1293,8 +1277,7 @@ export function concat<T>(schema: T): T;
/**
* Converts the type into an alternatives type where the conditions are merged into the type definition where:
*/
export function when(ref: string, options: WhenOptions): AlternativesSchema;
export function when(ref: Reference, options: WhenOptions): AlternativesSchema;
export function when(ref: string | Reference, options: WhenOptions): AlternativesSchema;
export function when(ref: Schema, options: WhenSchemaOptions): AlternativesSchema;
/**
+2 -2
View File
@@ -11,7 +11,7 @@ declare const exp: RegExp;
declare const obj: object;
declare const date: Date;
declare const err: Error;
declare const func: Function;
declare const func: () => void;
declare const numArr: number[];
declare const strArr: string[];
@@ -906,7 +906,7 @@ schema = Joi.alt(schema, anySchema, boolSchema);
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
schema = Joi.lazy(() => schema);
schema = Joi.lazy(() => schema, { once: true });
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
+1 -1
View File
@@ -20,4 +20,4 @@
"index.d.ts",
"joi-tests.ts"
]
}
}
+1 -3
View File
@@ -2,9 +2,7 @@
"extends": "dtslint/dt.json",
"rules": {
// All are TODOs
"ban-types": false,
"no-empty-interface": false,
"no-self-import": false,
"unified-signatures": false
"no-self-import": false
}
}
+1305
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+28
View File
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictFunctionTypes": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"types": [],
"paths": {
"joi": [
"joi/v13"
]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"joi-tests.ts"
]
}
+10
View File
@@ -0,0 +1,10 @@
{
"extends": "dtslint/dt.json",
"rules": {
// All are TODOs
"ban-types": false,
"no-empty-interface": false,
"no-self-import": false,
"unified-signatures": false
}
}