From 24b31d85632ea164dbc870ba378da38ddbce427e Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 3 Jun 2018 15:43:51 -0500 Subject: [PATCH] [joi] Update joi definitions to match current version (13.0.1 -> 13.3.0) * Add optional options package for string.hex * Add number.port option * Add object.append method * Add Joi.reach overloads with path argument having an array of strings * Update documentation strings for reach, keys --- types/joi/index.d.ts | 29 +++++++++++++++++++++++++---- types/joi/joi-tests.ts | 12 ++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/types/joi/index.d.ts b/types/joi/index.d.ts index 48e2a0327c..4fe6d1a431 100644 --- a/types/joi/index.d.ts +++ b/types/joi/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for joi v13.0.1 +// Type definitions for joi v13.3.0 // Project: https://github.com/hapijs/joi // Definitions by: Bart van der Schoor // Laurence Dougal Myers @@ -12,6 +12,7 @@ // Anjun Wang // Rafael Kallis // Conan Lai +// Peter Thorson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 @@ -106,6 +107,13 @@ export interface EmailOptions { minDomainAtoms?: number; } +export interface HexOptions { + /** + * hex decoded representation must be byte aligned + */ + byteAligned: boolean; +} + export interface IpOptions { /** * One or more IP address versions to validate against. Valid values: ipv4, ipv6, ipvfuture @@ -512,6 +520,11 @@ export interface NumberSchema extends AnySchema { * Requires the number to be negative. */ negative(): this; + + /** + * Requires the number to be a TCP port, so between 0 and 65535. + */ + port(): this; } export interface StringSchema extends AnySchema { @@ -624,7 +637,7 @@ export interface StringSchema extends AnySchema { /** * Requires the string value to be a valid hexadecimal string. */ - hex(): this; + hex(options?: HexOptions): this; /** * Requires the string value to be a valid hostname as per RFC1123. @@ -714,10 +727,15 @@ export interface ArraySchema extends AnySchema { export interface ObjectSchema extends AnySchema { /** - * Sets the allowed object keys. + * Sets or extends the allowed object keys. */ keys(schema?: SchemaMap): this; + /** + * Appends the allowed object keys. If schema is null, undefined, or {}, no changes will be applied. + */ + append(schema?: SchemaMap): this; + /** * Specifies the minimum number of keys in the object. */ @@ -1101,10 +1119,13 @@ export function ref(key: string, options?: ReferenceOptions): Reference; export function isRef(ref: any): ref is Reference; /** - * Get a sub-schema of an existing schema based on a path. Path separator is a dot (.). + * 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): T; +export function reach(schema: ObjectSchema, path: string[]): Schema; +export function reach(schema: ObjectSchema, path: string[]): T; /** * Creates a new Joi instance customized with the extension(s) you provide included. diff --git a/types/joi/joi-tests.ts b/types/joi/joi-tests.ts index 5ea9088a74..01147761d7 100644 --- a/types/joi/joi-tests.ts +++ b/types/joi/joi-tests.ts @@ -96,6 +96,12 @@ emailOpts = { minDomainAtoms: num }; // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- +let hexOpts: Joi.HexOptions = null; + +hexOpts = { byteAligned: bool }; + +// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- + let ipOpts: Joi.IpOptions = null; ipOpts = { version: str }; @@ -602,6 +608,7 @@ numSchema = numSchema.precision(num); numSchema = numSchema.multiple(num); numSchema = numSchema.positive(); numSchema = numSchema.negative(); +numSchema = numSchema.port(); namespace common { numSchema = numSchema.allow(x); @@ -659,6 +666,9 @@ objSchema = Joi.object(schemaMap); objSchema = objSchema.keys(); objSchema = objSchema.keys(schemaMap); +objSchema = objSchema.append(); +objSchema = objSchema.append(schemaMap); + objSchema = objSchema.min(num); objSchema = objSchema.max(num); objSchema = objSchema.length(num); @@ -801,6 +811,7 @@ strSchema = strSchema.guid(); strSchema = strSchema.guid({ version: ['uuidv1', 'uuidv2', 'uuidv3', 'uuidv4', 'uuidv5'] } as Joi.GuidOptions); strSchema = strSchema.guid({ version: 'uuidv4' }); strSchema = strSchema.hex(); +strSchema = strSchema.hex(hexOpts); strSchema = strSchema.hostname(); strSchema = strSchema.isoDate(); strSchema = strSchema.lowercase(); @@ -969,6 +980,7 @@ description = Joi.describe(schema); description = schema.describe(); schema = Joi.reach(objSchema, ''); +schema = Joi.reach(objSchema, []); const Joi2 = Joi.extend({ name: '', base: schema });