Socket
Socket
Sign inDemoInstall

@sinclair/typebox

Package Overview
Dependencies
Maintainers
1
Versions
324
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sinclair/typebox - npm Package Compare versions

Comparing version 0.16.7 to 0.17.0

16

package.json
{
"name": "@sinclair/typebox",
"version": "0.16.7",
"version": "0.17.0",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",

@@ -20,8 +20,9 @@ "keywords": [

"scripts": {
"clean": "smoke-task clean",
"build": "smoke-task build",
"start": "smoke-task example",
"test": "smoke-task spec"
"clean": "hammer task clean",
"build": "hammer task build",
"example": "hammer task example",
"test": "hammer task spec"
},
"devDependencies": {
"@sinclair/hammer": "^0.12.1",
"@types/chai": "^4.2.16",

@@ -34,7 +35,4 @@ "@types/mocha": "^8.2.2",

"mocha": "^8.3.2",
"smoke-run": "^1.1.2",
"smoke-task": "^1.1.2",
"typescript": "^4.1.2",
"typescript-bundle": "^1.0.16"
"typescript": "^4.1.2"
}
}

@@ -11,12 +11,4 @@ <div align='center'>

## Example
```typescript
import { Static, Type } from '@sinclair/typebox'
const T = Type.String() /* const T = { "type": "string" } */
type T = Static<typeof T> /* type T = string */
```
<a name="Install"></a>

@@ -30,2 +22,12 @@

## Usage
```typescript
import { Static, Type } from '@sinclair/typebox'
const T = Type.String() // const T = { "type": "string" }
type T = Static<typeof T> // type T = string
```
<a name="Overview"></a>

@@ -50,2 +52,3 @@

- [Options](#Options)
- [Additional Properties](#AdditionalProperties)
- [Strict](#Strict)

@@ -206,13 +209,12 @@ - [Extended Types](#Extended-Types)

│ x: Type.Number(), │ x: number, │ type: 'object', │
│ y: Type.Number() │ y: number │ additionalProperties: false, │
│ }) │ } │ properties: { │
│ │ │ x: { │
│ │ │ type: 'number' │
│ y: Type.Number() │ y: number │ properties: { │
│ }) │ } │ x: { │
│ │ │ type: 'number' │
│ │ │ }, │
│ │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['x', 'y'] │
│ │ │ } │
│ │ │ required: ['x', 'y'] │
│ │ │ } │
│ │ │ │

@@ -261,14 +263,14 @@ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

│ const T = Type.Intersect([ │ type T = { │ const T = { │
│ Type.Object({ │ x: number │ type: 'object', │
│ x: Type.Number() │ } & { │ additionalProperties: false, │
│ }), │ y: number │ properties: { │
│ Type.Object({ │ } │ x: { │
│ y: Type.Number() │ │ type: 'number' │
│ }) │ │ }, │
│ }) │ │ y: { │
│ │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['x', 'y'] │
│ Type.Object({ │ x: number │ type: 'object', │
│ x: Type.Number() │ } & { │ properties: { │
│ }), │ y: number │ x: { │
│ Type.Object({ │ } │ type: 'number' │
│ y: Type.Number() │ │ }, │
│ }) │ │ y: { │
│ }) │ │ type: 'number' │
│ │ │ } │
│ │ │ }, │
│ │ │ required: ['x', 'y'] │
│ │ │ } │
│ │ │ │
│ │ │ │

@@ -387,2 +389,36 @@ ├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤

<a name="AdditionalProperties"></a>
### Additional Properties
By default, schemas created with `Type.Object({...})` and `Type.Intersect([...])` are permissive of additional properties. This is inline with TypeScript's behaviour with respect to structural and polymorphic types as well as JSON schema rules around applying additive constraints (which aligns to a form of downstream polymorphism for the schema). This is quite a subtle concept, but consider the following where one might assume that the additional property `c` would raise a static assertion error.
#### TypeScript
```typescript
type T = { a: number, b: number }
function run(data: T) { }
const data = { a: 10, b: 10, c: 20 } as const // 'c' is an additional property
run(data) // this is fine
```
#### TypeBox
```typescript
const T = Type.Object({ a: Type.Number(), b: Type.Number() })
function run(data: Static<typeof T>) { }
const data = { a: 10, b: 10, c: 20 } as const // 'c' is an additional property
run(data) // this is fine
```
TypeBox aligns with TypeScript and JSON Schema semantics in this regard, however this may be undesirable for data received over the wire. You can disallow additional properties by applying the standard `additionalProperties` property as an option to the schema.
```typescript
const T = Type.Object({ a: Type.Number(), b: Type.Number() }, { additionalProperties: false })
```
<a name="Strict"></a>

@@ -389,0 +425,0 @@

@@ -61,2 +61,5 @@ export declare const ReadonlyOptionalModifier: unique symbol;

} & CustomOptions;
export declare type ObjectOptions = {
additionalProperties?: boolean;
} & CustomOptions;
export declare type TEnumType = Record<string, string | number>;

@@ -79,6 +82,5 @@ export declare type TKey = string | number | symbol;

type: 'object';
additionalProperties: false;
properties: T;
required?: string[];
} & CustomOptions;
} & ObjectOptions;
export declare type TUnion<T extends TSchema[]> = {

@@ -233,5 +235,5 @@ kind: typeof UnionKind;

/** `STANDARD` Creates a `object` schema with the given properties. */
Object<T extends TProperties>(properties: T, options?: CustomOptions): TObject<T>;
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
/** `STANDARD` Creates an intersection schema of the given object schemas. */
Intersect<T extends TObject<TProperties>[]>(items: [...T], options?: CustomOptions): TObject<IntersectObjectArray<T>>;
Intersect<T extends TObject<TProperties>[]>(items: [...T], options?: ObjectOptions): TObject<IntersectObjectArray<T>>;
/** `STANDARD` Creates a Union schema. */

@@ -238,0 +240,0 @@ Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T>;

@@ -118,6 +118,5 @@ "use strict";

const required = (required_names.length > 0) ? required_names : undefined;
const additionalProperties = false;
return (required) ?
{ ...options, kind: exports.ObjectKind, type: 'object', additionalProperties, properties, required } :
{ ...options, kind: exports.ObjectKind, type: 'object', additionalProperties, properties };
{ ...options, kind: exports.ObjectKind, type: 'object', properties, required } :
{ ...options, kind: exports.ObjectKind, type: 'object', properties };
}

@@ -129,6 +128,5 @@ /** `STANDARD` Creates an intersection schema of the given object schemas. */

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 };
? { ...options, type, kind: exports.ObjectKind, properties, required }
: { ...options, type, kind: exports.ObjectKind, properties };
}

@@ -135,0 +133,0 @@ /** `STANDARD` Creates a Union schema. */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc