New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

arri-validate

Package Overview
Dependencies
Maintainers
1
Versions
117
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arri-validate - npm Package Compare versions

Comparing version 0.32.2 to 0.33.0

dist/shared/arri-validate.330c7078.cjs

13

dist/index.d.ts

@@ -1,3 +0,3 @@

import { A as ASchemaOptions, a as ASchema, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, I as InferType, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationData, i as ARecordSchema, S as SafeResult, j as coerce, k as errors, l as InferSubType, p as parse, s as safeCoerce, m as safeParse, n as serialize, v as validate, o as SchemaValidator, q as SchemaMetadata, r as SCHEMA_METADATA } from './shared/arri-validate.59fde718.js';
export { J as InferObjectRawType, M as MaybeNullable, C as NumberType, N as NumberTypeValues, P as PartialBy, z as Resolve, w as ValidationError, u as ValueError, D as isAAraySchema, F as isADiscriminatorSchema, H as isAObjectSchema, G as isARecordSchema, B as isAScalarSchema, y as isASchema, E as isAStringEnumSchema, K as isObject, x as isValidationError, t as sanitizeJson } from './shared/arri-validate.59fde718.js';
import { A as ASchemaOptions, a as ASchema, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, I as InferType, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationData, i as ARecordSchema, j as ARefSchema, S as SafeResult, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate, q as SchemaValidator, r as SchemaMetadata, t as SCHEMA_METADATA } from './shared/arri-validate.b7c77149.js';
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, x as ValidationError, w as ValueError, E as isAAraySchema, G as isADiscriminatorSchema, J as isAObjectSchema, H as isARecordSchema, O as isARefSchema, C as isAScalarSchema, z as isASchema, F as isAStringEnumSchema, L as isObject, y as isValidationError, u as sanitizeJson } from './shared/arri-validate.b7c77149.js';
import { Schema } from 'jtd-utils';

@@ -134,2 +134,4 @@

declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput, TAdditionalProps>, TAdditionalProps>;
declare function parseObjectSchema<T>(schema: AObjectSchema<T>, input: unknown, data: ValidationData, coerce?: boolean): T | undefined;
declare function validateObjectSchema(schema: AObjectSchema, input: unknown): boolean;
/**

@@ -184,2 +186,4 @@ * Create an object schema using a subset of keys from another object schema

declare function recursive<T>(callback: (self: ARefSchema<T>) => AObjectSchema<T> | ADiscriminatorSchema<T>, options?: ASchemaOptions): AObjectSchema<T> | ADiscriminatorSchema<T>;
/**

@@ -271,2 +275,3 @@ * @example

declare const _namespace_record: typeof record;
declare const _namespace_recursive: typeof recursive;
declare const _namespace_safeCoerce: typeof safeCoerce;

@@ -284,3 +289,3 @@ declare const _namespace_safeParse: typeof safeParse;

declare namespace _namespace {
export { _namespace_any as any, _namespace_array as array, _namespace_boolean as boolean, _namespace_clone as clone, _namespace_coerce as coerce, _namespace_compile as compile, _namespace_discriminator as discriminator, _namespace_enumerator as enumerator, _namespace_errors as errors, _namespace_extend as extend, _namespace_float32 as float32, _namespace_float64 as float64, InferType as infer, InferSubType as inferSubType, _namespace_int16 as int16, _namespace_int32 as int32, _namespace_int64 as int64, _namespace_int8 as int8, _namespace_nullable as nullable, _namespace_number as number, _namespace_object as object, _namespace_omit as omit, _namespace_optional as optional, _namespace_parse as parse, _namespace_partial as partial, _namespace_pick as pick, _namespace_record as record, _namespace_safeCoerce as safeCoerce, _namespace_safeParse as safeParse, _namespace_serialize as serialize, _namespace_string as string, _namespace_stringEnum as stringEnum, _namespace_timestamp as timestamp, _namespace_uint16 as uint16, _namespace_uint32 as uint32, _namespace_uint64 as uint64, _namespace_uint8 as uint8, _namespace_validate as validate };
export { _namespace_any as any, _namespace_array as array, _namespace_boolean as boolean, _namespace_clone as clone, _namespace_coerce as coerce, _namespace_compile as compile, _namespace_discriminator as discriminator, _namespace_enumerator as enumerator, _namespace_errors as errors, _namespace_extend as extend, _namespace_float32 as float32, _namespace_float64 as float64, InferType as infer, InferSubType as inferSubType, _namespace_int16 as int16, _namespace_int32 as int32, _namespace_int64 as int64, _namespace_int8 as int8, _namespace_nullable as nullable, _namespace_number as number, _namespace_object as object, _namespace_omit as omit, _namespace_optional as optional, _namespace_parse as parse, _namespace_partial as partial, _namespace_pick as pick, _namespace_record as record, _namespace_recursive as recursive, _namespace_safeCoerce as safeCoerce, _namespace_safeParse as safeParse, _namespace_serialize as serialize, _namespace_string as string, _namespace_stringEnum as stringEnum, _namespace_timestamp as timestamp, _namespace_uint16 as uint16, _namespace_uint32 as uint32, _namespace_uint64 as uint64, _namespace_uint8 as uint8, _namespace_validate as validate };
}

@@ -326,2 +331,2 @@

export { type AAdaptedDiscriminatorSchema, type AAdaptedObjectSchema, type AAdaptedRecordSchema, type AAdaptedSchema, type AAdaptedSchemaMetadata, type AAdaptedSchemaValidator, AArraySchema, ADiscriminatorSchema, AObjectSchema, AObjectSchemaOptions, ARecordSchema, AScalarSchema, ASchema, ASchemaOptions, AStringEnumSchema, type CompiledValidator, InferObjectOutput, InferSubType, InferType, ResolveObject, SCHEMA_METADATA, SafeResult, SchemaMetadata, SchemaValidator, type ValidationAdapter, ValidationData, _namespace as a, any, array, boolean, clone, coerce, compile, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaParsingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, isAAdaptedSchema, nullable, number, object, omit, optional, parse, partial, pick, record, safeCoerce, safeParse, serialize, serializeObject, serializeSmallString, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate };
export { type AAdaptedDiscriminatorSchema, type AAdaptedObjectSchema, type AAdaptedRecordSchema, type AAdaptedSchema, type AAdaptedSchemaMetadata, type AAdaptedSchemaValidator, AArraySchema, ADiscriminatorSchema, AObjectSchema, AObjectSchemaOptions, ARecordSchema, ARefSchema, AScalarSchema, ASchema, ASchemaOptions, AStringEnumSchema, type CompiledValidator, InferObjectOutput, InferSubType, InferType, ResolveObject, SCHEMA_METADATA, SafeResult, SchemaMetadata, SchemaValidator, type ValidationAdapter, ValidationData, _namespace as a, any, array, boolean, clone, coerce, compile, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaParsingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, isAAdaptedSchema, nullable, number, object, omit, optional, parse, parseObjectSchema, partial, pick, record, recursive, safeCoerce, safeParse, serialize, serializeObject, serializeSmallString, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };

@@ -1,4 +0,21 @@

import { a as ASchema } from './shared/arri-validate.59fde718.js';
import { d as AObjectSchema, e as ADiscriminatorSchema, a as ASchema } from './shared/arri-validate.b7c77149.js';
import 'jtd-utils';
type RecursiveUnion = {
type: "CHILD";
data: RecursiveUnion;
} | {
type: "CHILDREN";
data: RecursiveUnion[];
} | {
type: "TEXT";
data: string;
} | {
type: "SHAPE";
data: {
width: number;
height: number;
};
};
declare const RecursiveUnion: AObjectSchema<RecursiveUnion, false> | ADiscriminatorSchema<RecursiveUnion>;
declare const validationTestSuites: Record<string, {

@@ -21,2 +38,2 @@ schema: ASchema;

export { parsingTestSuites, serializationTestSuites, validationTestSuites };
export { RecursiveUnion, parsingTestSuites, serializationTestSuites, validationTestSuites };
{
"name": "arri-validate",
"version": "0.32.2",
"version": "0.33.0",
"type": "module",

@@ -27,3 +27,3 @@ "license": "MIT",

"uncrypto": "^0.1.3",
"jtd-utils": "0.32.2"
"jtd-utils": "0.33.0"
},

@@ -30,0 +30,0 @@ "devDependencies": {

@@ -5,4 +5,18 @@ # Arri Validate

A lot of inspiration was taken from both [Typebox](https://github.com/sinclairzx81/typebox) and [Zod](https://github.com/colinhacks/zod) when designing this library
A lot of inspiration was taken from both [Typebox](https://github.com/sinclairzx81/typebox) and [Zod](https://github.com/colinhacks/zod) when designing this library.
## Project Philosophy
The goals of this project are as follows:
- Portable type definitions
- High performance validation, parsing, and serialization
- Consistent error reporting for parsing and serialization errors
I am not looking to support every feature of Typescript's type system or even every possible representation of JSON. The goal is that the data models defined through this library can be used as a source of truth across multiple programming languages. Both JSON and Typescript have to be limited to accomplish this.
### Adherence to RFC 8927
To represent the data-models in a language agnostic way this library heavily relies on JSON Type Definition (JTD). However, this library does not strictly comply with the JTD specification. The reason for this is because JTD does not support 64-bit integers. I believe sharing large integers across languages is a huge pain point especially when going to and from Javascript. For this reason alone, I have opted to break away from the JTD spec and add support for `int64` and `uint64`. So while I have no intention to break further away from the spec I am open to it if a large enough issue arises (in my view). If you use this library be aware that I'm using a superset of JTD rather than a strict spec compliant implementation.
## Table of Contents

@@ -19,2 +33,3 @@

- [Discriminated Unions](#discriminated-unions)
- [Recursive Types](#recursive-types)
- [Modifiers](#modifiers)

@@ -36,2 +51,3 @@ - [Optional](#optional)

- [Compiled Validators](#compiled-validators)
- [Metadata](#metadata)
- [Benchmarks](#benchmarks)

@@ -76,16 +92,2 @@ - [Development](#development)

## Project Philosophy
The goals of this project are as follows:
- Portable type definitions
- High performance validation, parsing, and serialization
- Consistent error reporting for parsing and serialization errors
I am not looking to support every feature of Typescript's type system or even every possible representation of JSON. The goal is that the data models defined through this library can be used as a source of truth across multiple programming languages. Both JSON and Typescript have to be limited to accomplish this.
### Adherence to RFC 8927
To represent the data-models in a language agnostic way this library heavily relies on JSON Type Definition (JTD). However, this library does not strictly comply with the JTD specification. The reason for this is because JTD does not support 64-bit integers. I believe sharing large integers across languages is a huge pain point especially when going to and from Javascript. For this reason alone, I have opted to break away from the JTD spec and add support for `int64` and `uint64`. So while I have no intention to break further away from the spec I am open to it if a large enough issue arises (in my view). If you use this library be aware that I'm using a superset of JTD rather than a strict spec compliant implementation.
## Supported Types

@@ -194,6 +196,50 @@

}
}
},
"additionalProperties": true
}
```
#### Strict Mode
By default arri-validate will ignore and strip out any additional properties when validating objects. If you want validation to fail when additional properties are present then modify the `additionalProperties` option.
```ts
const UserStrict = a.object(
{
id: a.string(),
name: a.string(),
created: a.timestamp(),
},
{
additionalProperties: false,
},
);
a.parse(UserStrict, {
id: "1",
name: "johndoe",
created: new Date(),
bio: "my name is joe",
}); // fails parsing because of the additional field "bio"
```
**Outputted JTD**
```json
{
"properties": {
"id": {
"type": "string"
},
"email": {
"type": "string"
},
"created": {
"type": "timestamp"
}
},
"additionalProperties": false
}
```
### Records / Maps

@@ -276,3 +322,4 @@

}
}
},
"additionalProperties": true
},

@@ -284,3 +331,4 @@ "CIRCLE": {

}
}
},
"additionalProperties": true
}

@@ -291,2 +339,81 @@ }

### Recursive Types
You can define recursive schemas by using the `a.recursive` helper. This function accepts another function that outputs an object schema or a discriminator schema.
An important thing to note is that type inference doesn't work correctly for Recursive schemas. In order to satisfy Typescript you will need to define the type and then pass it to the function as a generic.
Additionally it is recommended to define an ID for any recursive schemas. If one is not specified arri will auto generate one.
---
_If some TS wizard knows how to get type inference to work automatically for these recursive schemas, feel free to open a PR although I fear it will require a major refactor the existing type system._
**Usage**
```ts
// the recursive type must be defined first
type BinaryTree = {
left: BinaryTree | null;
right: BinaryTree | null;
};
// pass the type to the helper
const BinaryTree = a.recursive<BinaryTree>(
(self) =>
// the resulting schema must be an object or discriminator
// it also must match the type you pass into the generic parameter
// or TS will yell at you
a.object({
left: a.nullable(self),
right: a.nullable(self),
}),
{
id: "BinaryTree",
},
);
a.validate(BinaryTree, {
left: {
left: null,
right: {
left: null,
right: null,
},
},
right: null,
}); // true
a.validate(BinaryTree, {
left: {
left: null,
right: {
left: true,
right: null,
},
},
right: null,
}); // false
```
**Outputted JTD**
```json
{
"properties": {
"left": {
"ref": "BinaryTree",
"nullable": true
},
"right": {
"ref": "BinaryTree",
"nullable": true
}
},
"additionalProperties": true,
"metadata": {
"id": "BinaryTree"
}
}
```
## Modifiers

@@ -594,8 +721,6 @@

In most cases, the compiled validators will be much faster than the standard utilities. However there is some overhead with compiling the schemas so ideally each validator would be compiled once.
In most cases, the compiled validators will be much faster than the standard utilities. However there is some overhead with compiling the schemas so ideally each validator would be compiled once. Additionally the resulting methods make use of eval so they can only be used in an environment that you control such as a backend server. They WILL NOT work in a browser environment.
Additionally the resulting methods make use of eval so they can only be used in an environment that you control such as a backend server. They WILL NOT work in a browser environment.
You can also use `a.compile` for code generation. The compiler result gives you access to the generated function bodies.
You can use `a.compile` for code generation. The compiler result gives you access to the generated function bodies.
```ts

@@ -607,5 +732,86 @@ $$User.compiledCode.validate; // the generated validation code

## Metadata
Metadata is used during cross-language code generation. Arri schemas allow you to specify the following metadata fields:
- id - Will be used as the type name in any arri client generators
- description - Will be added as a description comment above any generated types
- isDeprecated - Will mark any generated code with the deprecation annotation of target language
### Examples
A schema with this metadata:
```ts
const BookSchema = a.object(
{
title: a.string(),
author: a.string(),
publishDate: a.timestamp(),
},
{
id: "Book",
description: "This is a book",
},
);
```
will produce types that look something like this during codegen.
**Typescript**
```ts
/**
* This is a book
*/
interface Book {
title: string;
author: string;
publishDate: Date;
}
```
**Rust**
```rust
/// This is a book
struct Book {
title: String,
author: String,
publish_date: DateTime<FixedOffset>
}
```
**Dart**
```dart
/// This is a book
class Book {
final String title;
final String author;
final DateTime publishDate;
const Book({
required this.title,
required this.author,
required this.publishDate,
});
}
```
**Kotlin**
```kotlin
/**
* This is a book
*/
data class Book(
val title: String,
val author: String,
val publishDate: Instant,
)
```
## Benchmarks
_Last Updated: 2024-03-14_
_Last Updated: 2024-03-19_

@@ -657,11 +863,11 @@ All benchmarks were run on my personal desktop. You can view the methodology used in [./benchmarks/src](./benchmark/src).

| ---------------------------- | ----------- |
| **Arri (Compiled)** | 135,577,679 |
| Typebox (Compiled) | 59,141,334 |
| Ajv -JTD (Compiled) | 38,308,645 |
| Ajv - JTD | 30,481,986 |
| Ajv - JSON Schema (Compiled) | 12,266,309 |
| Ajv -JSON Schema | 10,430,898 |
| **Arri** | 2,243,081 |
| Typebox | 98,2233 |
| Zod | 52,9865 |
| **Arri (Compiled)** | 122,753,978 |
| Typebox (Compiled) | 63,131,651 |
| Ajv -JTD (Compiled) | 37,814,896 |
| Ajv - JTD | 29,402,413 |
| Ajv - JSON Schema (Compiled) | 12,003,432 |
| Ajv -JSON Schema | 10,057,501 |
| **Arri** | 2,131,823 |
| Typebox | 93,5599 |
| Zod | 52,1357 |

@@ -672,6 +878,6 @@ #### Parsing

| ------------------- | ------- |
| JSON.parse | 777,989 |
| **Arri (Compiled)** | 729,225 |
| **Arri** | 376,368 |
| Ajv -JTD (Compiled) | 245,730 |
| JSON.parse | 749,534 |
| **Arri (Compiled)** | 728,382 |
| **Arri** | 378,175 |
| Ajv -JTD (Compiled) | 241,107 |

@@ -682,7 +888,7 @@ #### Serialization

| ------------------------------------------ | --------- |
| **Arri (Compiled)** | 4,892,713 |
| **Arri (Compiled) Validate and Serialize** | 4,710,745 |
| Ajv - JTD (Compiled) | 2,222,308 |
| JSON.stringify | 1,318,842 |
| Arri | 582,747 |
| **Arri (Compiled)** | 4,272,430 |
| **Arri (Compiled) Validate and Serialize** | 3,846,453 |
| Ajv - JTD (Compiled) | 2,012,894 |
| JSON.stringify | 938,289 |
| Arri | 481,985 |

@@ -693,8 +899,10 @@ #### Coercion

| -------- | ------- |
| **Arri** | 839,642 |
| Typebox | 212,706 |
| Zod | 488,505 |
| **Arri** | 818,963 |
| Zod | 466,092 |
| Typebox | 209,363 |
### Integers
The following benchmarks measure how quickly each library operates on a single integer value.
#### Validation

@@ -704,11 +912,11 @@

| ---------------------------- | ----------- |
| **Arri** | 210,933,339 |
| **Arri (Compiled)** | 207,119,519 |
| Ajv - JSON Schema (Compiled) | 193,992,382 |
| Ajv - JTD (Compiled) | 169,603,655 |
| Typebox (Compiled) | 113,166,065 |
| Ajv - JSON Schema | 55,942,777 |
| Ajv - JTD | 51,083,383 |
| Typebox | 45,753,844 |
| Zod | 1,163,445 |
| Ajv - JSON Schema (Compiled) | 329,332,736 |
| **Arri (Compiled)** | 201,644,167 |
| **Arri** | 186,634,732 |
| Ajv - JTD (Compiled) | 151,044,902 |
| Typebox (Compiled) | 110,692,029 |
| Ajv - JTD | 48,200,004 |
| Ajv - JSON Schema | 47,840,571 |
| Typebox | 42,363,980 |
| Zod | 1,266,268 |

@@ -719,7 +927,25 @@ #### Parsing

| -------------------- | ----------- |
| **Arri (Compiled)** | 153,161,724 |
| **Arri** | 138,649,162 |
| JSON.parse() | 19,045,882 |
| Ajv - JTD (Compiled) | 9,363,809 |
| **Arri (Compiled)** | 138,189,123 |
| **Arri** | 136,995,619 |
| JSON.parse() | 19,911,721 |
| Ajv - JTD (Compiled) | 8,996,081 |
#### Serialization
| Library | op/s |
| -------------------- | ----------- |
| Ajv - JTD (Compiled) | 198,980,679 |
| **Arri (Compiled)** | 190,386,426 |
| **Arri** | 114,799,692 |
| JSON.stringify | 21,433,854 |
#### Coercion
| Library | op/s |
| ----------------- | ----------- |
| Arri | 117,854,219 |
| TypeBox | 34,633,126 |
| Ajv - JSON Schema | 28,016,735 |
| Zod | 1,586,546 |
## Development

@@ -726,0 +952,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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