diff --git a/index.d.ts b/index.d.ts index da692e4..4df0819 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -type AnySchema = NullSchema | BooleanSchema | NumberSchema | StringSchema | AnyEnumSchema | AnyArraySchema | AnyObjectSchema +type AnySchema = NullSchema | BooleanSchema | NumberSchema | StringSchema | AnyEnumSchema | AnyArraySchema | AnyObjectSchema | AnyAllOptionalObjectSchema type StringKeys = (keyof T) & string interface NullSchema { @@ -36,6 +36,13 @@ interface ObjectSchema, Required ex required: Required[] } +interface AnyAllOptionalObjectSchema extends AllOptionalObjectSchema> {} +interface AllOptionalObjectSchema> { + additionalProperties?: boolean + type: 'object' + properties: Properties +} + interface ArrayFromSchema extends Array> {} type ObjectFromSchema, Required extends StringKeys> = { @@ -50,6 +57,7 @@ type TypeFromSchema = ( : Schema extends StringSchema ? string : Schema extends ArraySchema ? ArrayFromSchema : Schema extends ObjectSchema ? ObjectFromSchema + : Schema extends AllOptionalObjectSchema ? ObjectFromSchema : never ) diff --git a/test/typings.ts b/test/typings.ts index 6244d66..4ccf903 100644 --- a/test/typings.ts +++ b/test/typings.ts @@ -186,3 +186,21 @@ if (metricValidator(input)) { assertType<'page-view'>(input.name) assertType(input.page) } + +const noRequiredFieldsValidator = createValidator({ + type: 'object', + properties: { + a: { type: 'string' }, + b: { type: 'string' }, + c: { type: 'string' } + } +}) + +if (noRequiredFieldsValidator(input)) { + if (typeof input.a !== 'string') assertType(input.a) + if (typeof input.b !== 'string') assertType(input.b) + if (typeof input.c !== 'string') assertType(input.c) + if (typeof input.a !== 'undefined') assertType(input.a) + if (typeof input.b !== 'undefined') assertType(input.b) + if (typeof input.c !== 'undefined') assertType(input.c) +}