From a87899033fc80a67add35a391b34559e096fd97a Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Mon, 9 Sep 2019 15:26:39 +0200 Subject: [PATCH] refactor: make joi.meta() arguments type-safe --- garden-service/src/config/common.ts | 54 ++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/garden-service/src/config/common.ts b/garden-service/src/config/common.ts index 1de130d720..0936636d9f 100644 --- a/garden-service/src/config/common.ts +++ b/garden-service/src/config/common.ts @@ -43,13 +43,59 @@ interface JoiPathParams { } // Extend the Joi module with our custom rules -export interface CustomStringSchema extends Joi.StringSchema { - gitUrl: (params: JoiGitUrlParams) => CustomStringSchema - posixPath: (params?: JoiPathParams) => CustomStringSchema +interface MetadataKeys { + internal?: boolean + deprecated?: boolean + extendable?: boolean } +// Unfortunately we need to explicitly extend each type (just extending the AnySchema doesn't work). declare module "@hapi/joi" { - export function string(): CustomStringSchema + export interface AnySchema { + meta(keys: MetadataKeys): this + } + + export interface ArraySchema { + meta(keys: MetadataKeys): this + } + + export interface AlternativesSchema { + meta(keys: MetadataKeys): this + } + + export interface BinarySchema { + meta(keys: MetadataKeys): this + } + + export interface BooleanSchema { + meta(keys: MetadataKeys): this + } + + export interface DateSchema { + meta(keys: MetadataKeys): this + } + + export interface FunctionSchema { + meta(keys: MetadataKeys): this + } + + export interface NumberSchema { + meta(keys: MetadataKeys): this + } + + export interface ObjectSchema { + meta(keys: MetadataKeys): this + } + + export interface StringSchema { + meta(keys: MetadataKeys): this + gitUrl: (params: JoiGitUrlParams) => this + posixPath: (params?: JoiPathParams) => this + } + + export interface LazySchema { + meta(keys: MetadataKeys): this + } } export const joi: Joi.Root = Joi.extend({