Comparing version 2.0.3 to 2.1.0
@@ -9,3 +9,3 @@ import { Validator } from './validate'; | ||
export declare type ObjectDefinition = Record<string, ObjectDefinitionValue<any>>; | ||
declare type ObjectReturnType<T> = T extends ObjectDefinitionValue<infer U> ? U : never; | ||
export declare type ObjectReturnType<T> = T extends ObjectDefinitionValue<infer U> ? U : never; | ||
declare type FilterObject<T, C> = { | ||
@@ -19,9 +19,16 @@ [k in keyof T]: T[k] extends C ? k : never; | ||
declare type NonMatchingKeys<T, C> = NotFilterObject<T, C>[keyof T]; | ||
declare type MandatoryKeys<D> = NonMatchingKeys<D, { | ||
optional: true; | ||
}>; | ||
declare type OptionalKeys<D> = MatchingKeys<D, { | ||
optional: true; | ||
}>; | ||
export declare type ObjectResult<D> = { | ||
[k in MandatoryKeys<D>]: ObjectReturnType<D[k]>; | ||
} & { | ||
[k in OptionalKeys<D>]?: ObjectReturnType<D[k]>; | ||
}; | ||
export declare function object<D extends ObjectDefinition>(definition: D, { allowExcessProperties }?: { | ||
allowExcessProperties?: boolean; | ||
}): (value: unknown) => { [k in NonMatchingKeys<D, { | ||
optional: true; | ||
}>]: ObjectReturnType<D[k]>; } & { [k_1 in MatchingKeys<D, { | ||
optional: true; | ||
}>]?: ObjectReturnType<D[k_1]> | undefined; }; | ||
}): (value: unknown) => ObjectResult<D>; | ||
export declare function defaultTo<R>(validator: Validator<R>, _default: R | (() => R)): ObjectOptions<R>; | ||
@@ -28,0 +35,0 @@ export declare function optional<R>(validator: Validator<R>): { |
{ | ||
"name": "fefe", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Validate, sanitize and transform values with proper types.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,3 +14,3 @@ import { FefeError } from './errors' | ||
type ObjectReturnType<T> = T extends ObjectDefinitionValue<infer U> | ||
export type ObjectReturnType<T> = T extends ObjectDefinitionValue<infer U> | ||
? U | ||
@@ -27,2 +27,6 @@ : never | ||
export type ObjectResult<D> = | ||
{ [k in MandatoryKeys<D>]: ObjectReturnType<D[k]> } & | ||
{ [k in OptionalKeys<D>]?: ObjectReturnType<D[k]> } | ||
export function object<D extends ObjectDefinition> ( | ||
@@ -49,5 +53,4 @@ definition: D, | ||
const validated = {} as | ||
{ [k in MandatoryKeys<D>]: ObjectReturnType<D[k]> } & | ||
{ [k in OptionalKeys<D>]?: ObjectReturnType<D[k]> } | ||
const validated = {} as ObjectResult<D> | ||
Object.entries(definition).forEach(([key, definitionValue]: [string, ObjectDefinitionValue<any>]) => { | ||
@@ -54,0 +57,0 @@ const options: ObjectOptions<any> = typeof definitionValue === 'object' ? |
Sorry, the diff of this file is not supported yet
103756
1574