@sinclair/typebox
Advanced tools
Comparing version 0.16.3 to 0.16.4
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.16.3", | ||
"version": "0.16.4", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -229,2 +229,6 @@ export declare const ReadonlyOptionalModifier: unique symbol; | ||
Object<T extends TProperties>(properties: T, options?: CustomOptions): TObject<T>; | ||
/** `STANDARD` Creates an intersection schema of the given object schemas. */ | ||
Intersect<T extends TObject<TProperties>[]>(items: [...T], options?: CustomOptions): TObject<IntersectObjectArray<T>>; | ||
/** `STANDARD` Creates a Union schema. */ | ||
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T>; | ||
/** `STANDARD` Creates a `{ [key: string]: T }` schema. */ | ||
@@ -254,8 +258,4 @@ Dict<T extends TSchema>(item: T, options?: DictOptions): TDict<T>; | ||
Any(options?: CustomOptions): TAny; | ||
/** `STANDARD` Creates a Union schema. */ | ||
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T>; | ||
/** `STANDARD` Creates a `keyof` schema. */ | ||
KeyOf<T extends TObject<TProperties>>(schema: T, options?: CustomOptions): TKeyOf<ObjectPropertyKeys<T>[]>; | ||
/** `STANDARD` Creates an intersection schema of the given object schemas. */ | ||
Intersect<T extends TObject<TProperties>[]>(items: [...T], options?: CustomOptions): TObject<IntersectObjectArray<T>>; | ||
/** `STANDARD` Make all properties in schema object required. */ | ||
@@ -262,0 +262,0 @@ Required<T extends TObject<TProperties>>(schema: T, options?: CustomOptions): TObject<TRequired<T['properties']>>; |
@@ -64,18 +64,22 @@ "use strict"; | ||
// ------------------------------------------------------------------------ | ||
// Clone | ||
// Utility | ||
// ------------------------------------------------------------------------ | ||
function isObject(object) { | ||
return typeof object === 'object' && object !== null && !Array.isArray(object); | ||
} | ||
function isArray(object) { | ||
return typeof object === 'object' && object !== null && Array.isArray(object); | ||
} | ||
function clone(object) { | ||
if (typeof object === 'object' && object !== null && !Array.isArray(object)) { | ||
return Object.keys(object).reduce((acc, key) => { | ||
acc[key] = clone(object[key]); | ||
return acc; | ||
}, {}); | ||
} | ||
else if (typeof object === 'object' && object !== null && Array.isArray(object)) { | ||
if (isObject(object)) | ||
return Object.keys(object).reduce((acc, key) => ({ ...acc, [key]: clone(object[key]) }), {}); | ||
if (isArray(object)) | ||
return object.map((item) => clone(item)); | ||
} | ||
else { | ||
return object; | ||
} | ||
return object; | ||
} | ||
function distinct(keys) { | ||
return Object.keys(keys.reduce((acc, key) => { | ||
return { ...acc, [key]: null }; | ||
}, {})); | ||
} | ||
// ------------------------------------------------------------------------ | ||
@@ -120,2 +124,16 @@ // TypeBuilder | ||
} | ||
/** `STANDARD` Creates an intersection schema of the given object schemas. */ | ||
Intersect(items, options = {}) { | ||
const type = 'object'; | ||
const properties = items.reduce((acc, object) => ({ ...acc, ...object['properties'] }), {}); | ||
const required = distinct(items.reduce((acc, object) => object['required'] ? [...acc, ...object['required']] : acc, [])); | ||
const additionalProperties = false; | ||
return (required.length > 0) | ||
? { ...options, type, kind: exports.ObjectKind, additionalProperties, properties, required } | ||
: { ...options, type, kind: exports.ObjectKind, additionalProperties, properties }; | ||
} | ||
/** `STANDARD` Creates a Union schema. */ | ||
Union(items, options = {}) { | ||
return { ...options, kind: exports.UnionKind, anyOf: items }; | ||
} | ||
/** `STANDARD` Creates a `{ [key: string]: T }` schema. */ | ||
@@ -178,6 +196,2 @@ Dict(item, options = {}) { | ||
} | ||
/** `STANDARD` Creates a Union schema. */ | ||
Union(items, options = {}) { | ||
return { ...options, kind: exports.UnionKind, anyOf: items }; | ||
} | ||
/** `STANDARD` Creates a `keyof` schema. */ | ||
@@ -188,10 +202,2 @@ KeyOf(schema, options = {}) { | ||
} | ||
/** `STANDARD` Creates an intersection schema of the given object schemas. */ | ||
Intersect(items, options = {}) { | ||
const type = 'object'; | ||
const additionalProperties = false; | ||
const properties = items.reduce((acc, object) => ({ ...acc, ...object['properties'] }), {}); | ||
const required = items.reduce((acc, object) => object['required'] ? [...acc, ...object['required']] : acc, []); | ||
return { ...options, type, kind: exports.ObjectKind, additionalProperties, properties, required: [...new Set(required)] }; | ||
} | ||
/** `STANDARD` Make all properties in schema object required. */ | ||
@@ -198,0 +204,0 @@ Required(schema, options = {}) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76591
567