@arrirpc/schema 0.73.0 → 0.74.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,84 +1,80 @@
1
1
  import { Type } from '@arrirpc/type-defs';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
3
 
4
- declare function sanitizeJson(json: string): string;
5
- /**
6
- * Check if the input matches the specified schema. This is a type guard.
7
- */
8
- declare function validate<T = any>(schema: ASchema<T>, input: unknown): input is T;
9
- /**
10
- * Parse a JSON string or the result of JSON.parse(). Throws a ValidationError if parsing fails.
11
- */
12
- declare function parse<T = any>(schema: ASchema<T>, input: unknown): T;
13
- /**
14
- * Parse a value without throwing an error.
15
- */
16
- declare function safeParse<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
17
- /**
18
- * Try to convert input to match the specified schema. Throws a ValidationError if conversion fails.
19
- */
20
- declare function coerce<T = any>(schema: ASchema<T>, input: unknown): T;
21
- type SafeResult<T> = {
22
- success: true;
23
- value: T;
24
- } | {
25
- success: false;
26
- error: ValidationError;
27
- };
28
- /**
29
- * Convert a value into the specified schema without throwing an error.
30
- */
31
- declare function safeCoerce<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
32
- /**
33
- * Serialize a value into a JSON string
34
- */
35
- declare function serialize<T = any>(schema: ASchema<T>, input: T): string;
36
- declare function errors(schema: ASchema, input: unknown): ValueError[];
37
4
  interface ValueError {
5
+ /**
6
+ * Error message
7
+ */
8
+ message: string;
9
+ /**
10
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to where in the input is invalid
11
+ */
38
12
  instancePath: string;
39
- schemaPath: string;
40
- message?: string;
13
+ /**
14
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to the part of the schema that rejected the input
15
+ */
16
+ schemaPath?: string;
17
+ /**
18
+ * Arbitrary data that can be included with the error
19
+ */
41
20
  data?: any;
42
21
  }
43
- declare class ValidationError extends Error {
22
+ declare function isValueError(input: unknown): input is ValueError;
23
+ declare function errorMessageFromErrors(errors: ValueError[]): string;
24
+ declare class ValidationException extends Error {
44
25
  errors: ValueError[];
45
26
  constructor(options: {
46
- message: string;
27
+ message?: string;
47
28
  errors: ValueError[];
48
29
  });
49
30
  }
50
- declare function isValidationError(input: unknown): input is ValidationError;
31
+ declare function isValidationException(input: unknown): input is ValidationException;
32
+ type Result<T> = ResultSuccess<T> | ResultFailure;
33
+ type ResultSuccess<T> = {
34
+ success: true;
35
+ value: T;
36
+ };
37
+ type ResultFailure = {
38
+ success: false;
39
+ errors: ValueError[];
40
+ };
51
41
 
52
- declare const SCHEMA_METADATA: unique symbol;
53
42
  type MaybeNullable<T = any, TIsNullable extends boolean = false> = TIsNullable extends true ? T | null : T;
54
43
  interface ValidationContext {
55
44
  instancePath: string;
56
45
  schemaPath: string;
57
46
  errors: ValueError[];
47
+ exitOnFirstError: boolean;
48
+ depth: number;
49
+ maxDepth: number;
58
50
  discriminatorKey?: string;
59
51
  discriminatorValue?: string;
60
52
  }
53
+ declare function newValidationContext(exitOnFirstError?: boolean, maxDepth?: number): ValidationContext;
54
+ declare const VALIDATOR_KEY: " Symbol.for(arri/schema/validator/v1)";
61
55
  interface SchemaValidator<T> {
62
56
  output: T;
63
57
  optional?: boolean;
58
+ validate: (input: unknown) => input is T;
64
59
  parse: (input: unknown, context: ValidationContext) => T | undefined;
65
60
  coerce: (input: unknown, context: ValidationContext) => T | undefined;
66
- serialize: (input: T, context: ValidationContext) => string;
67
- validate: (input: unknown) => input is T;
68
- _isAdapted?: boolean;
61
+ serialize: (input: T, context: ValidationContext) => string | undefined;
69
62
  }
70
- interface SchemaMetadata<T> {
63
+ interface SchemaMetadata {
71
64
  [key: string]: any;
72
65
  id?: string;
73
66
  description?: string;
74
67
  isDeprecated?: boolean;
75
- [SCHEMA_METADATA]: SchemaValidator<T>;
76
68
  }
77
- interface ASchema<T = any> extends StandardSchemaV1<T> {
78
- metadata: SchemaMetadata<T>;
69
+ type WithAdapters<T = any> = StandardSchemaV1<T>;
70
+ interface ASchema<T = any> {
71
+ metadata?: SchemaMetadata;
79
72
  nullable?: boolean;
73
+ [VALIDATOR_KEY]: SchemaValidator<T>;
80
74
  }
75
+ type ASchemaStrict<TSchema extends ASchema> = Omit<TSchema, typeof VALIDATOR_KEY>;
81
76
  declare function isASchema(input: unknown): input is ASchema;
77
+ type ASchemaWithAdapters<T = any> = ASchema<T> & WithAdapters<T>;
82
78
  interface ASchemaOptions {
83
79
  id?: string;
84
80
  description?: string;
@@ -89,7 +85,7 @@ type ResolveObject<T> = Resolve<{
89
85
  [k in keyof T]: T[k];
90
86
  }>;
91
87
  type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
92
- type InferType<TInput extends ASchema<any>> = Resolve<TInput['metadata'][typeof SCHEMA_METADATA]['output']>;
88
+ type InferType<TInput extends ASchema<any>> = Resolve<TInput[typeof VALIDATOR_KEY]['output']>;
93
89
  /**
94
90
  * Infer a specific subtype of a discriminated union.
95
91
  * In order for this to work you must first infer the union type using `a.infer`
@@ -125,31 +121,37 @@ interface AScalarSchema<T extends Type | NumberType = any, TVal = any> extends A
125
121
  type: T;
126
122
  }
127
123
  declare function isAScalarSchema(input: unknown): input is AScalarSchema;
124
+ type AScalarSchemaWithAdapters<T extends Type | NumberType = any, TVal = any> = AScalarSchema<T, TVal> & WithAdapters<TVal>;
128
125
  declare const NumberTypeValues: readonly ["float32", "float64", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"];
129
126
  type NumberType = (typeof NumberTypeValues)[number];
130
127
  interface AArraySchema<TInnerSchema extends ASchema<any> = any> extends ASchema<Array<InferType<TInnerSchema>>> {
131
128
  elements: TInnerSchema;
132
129
  }
133
130
  declare function isAAraySchema(input: unknown): input is AArraySchema;
131
+ type AArraySchemaWithAdapters<TInnerSchema extends ASchema<any> = any> = AArraySchema<TInnerSchema> & WithAdapters<InferType<TInnerSchema>[]>;
134
132
  interface AStringEnumSchema<TValues extends string[]> extends ASchema<TValues[number]> {
135
133
  enum: TValues;
136
134
  }
137
135
  declare function isAStringEnumSchema(input: unknown): input is AStringEnumSchema<any>;
136
+ type AStringEnumSchemaWithAdapters<T extends string[]> = AStringEnumSchema<T> & WithAdapters<T[number]>;
138
137
  interface ADiscriminatorSchema<T> extends ASchema<T> {
139
138
  discriminator: string;
140
139
  mapping: Record<string, AObjectSchema<any>>;
141
140
  }
142
141
  declare function isADiscriminatorSchema(input: unknown): input is ADiscriminatorSchema<any>;
143
- interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boolean = false> extends ASchema<MaybeNullable<Record<string, InferType<TInnerSchema>>, TNullable>> {
142
+ type ADiscriminatorSchemaWithAdapters<T> = ADiscriminatorSchema<T> & WithAdapters<T>;
143
+ interface ARecordSchema<TInnerSchema extends ASchema<any>> extends ASchema<Record<string, InferType<TInnerSchema>>> {
144
144
  values: TInnerSchema;
145
145
  }
146
- declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
146
+ declare function isARecordSchema(input: unknown): input is ARecordSchema<any>;
147
+ type ARecordSchemaWithAdapters<TInnerSchema extends ASchema<any>> = ARecordSchema<TInnerSchema> & WithAdapters<Record<string, InferType<TInnerSchema>>>;
147
148
  interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
148
149
  properties: Record<string, ASchema>;
149
150
  optionalProperties?: Record<string, ASchema>;
150
151
  strict?: TStrict;
151
152
  }
152
153
  declare function isAObjectSchema(input: unknown): input is AObjectSchema;
154
+ type AObjectSchemaWithAdapters<T = any, TStrict extends boolean = false> = AObjectSchema<T, TStrict> & WithAdapters<T>;
153
155
  interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
154
156
  /**
155
157
  * Allow this object to include additional properties not specified here
@@ -158,12 +160,13 @@ interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends
158
160
  }
159
161
  type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
160
162
  type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
161
- [TKey in keyof TInput]: TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['optional'] extends true ? TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'] | undefined : TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'];
163
+ [TKey in keyof TInput]: TInput[TKey][typeof VALIDATOR_KEY]['optional'] extends true ? TInput[TKey][typeof VALIDATOR_KEY]['output'] | undefined : TInput[TKey][typeof VALIDATOR_KEY]['output'];
162
164
  } : never;
163
165
  declare function isObject(input: unknown): input is Record<any, any>;
164
166
  interface ARefSchema<T> extends ASchema<T> {
165
167
  ref: string;
166
168
  }
167
169
  declare function isARefSchema(input: unknown): input is ARefSchema<any>;
170
+ type ARefSchemaWithAdapters<T> = ARefSchema<T> & WithAdapters<T>;
168
171
 
169
- export { type ASchema as A, type Resolve as B, isAScalarSchema as C, type NumberType as D, isAAraySchema as E, isAStringEnumSchema as F, isADiscriminatorSchema as G, isARecordSchema as H, type InferType as I, isAObjectSchema as J, type InferObjectRawType as K, isObject as L, type MaybeNullable as M, NumberTypeValues as N, isARefSchema as O, type PartialBy as P, type ResolveObject as R, type SafeResult as S, type ValidationContext as V, type ASchemaOptions as a, type AArraySchema as b, type AScalarSchema as c, type AObjectSchema as d, type ADiscriminatorSchema as e, type AStringEnumSchema as f, type AObjectSchemaOptions as g, type InferObjectOutput as h, type ARecordSchema as i, type ARefSchema as j, coerce as k, errors as l, type InferSubType as m, safeParse as n, serialize as o, parse as p, sanitizeJson as q, type ValueError as r, safeCoerce as s, ValidationError as t, isValidationError as u, validate as v, SCHEMA_METADATA as w, type SchemaValidator as x, type SchemaMetadata as y, isASchema as z };
172
+ export { type ASchema as A, type Resolve as B, type AScalarSchema as C, isAScalarSchema as D, type NumberType as E, isAAraySchema as F, type AArraySchemaWithAdapters as G, type AStringEnumSchema as H, type InferType as I, isAStringEnumSchema as J, type ADiscriminatorSchema as K, isADiscriminatorSchema as L, type MaybeNullable as M, NumberTypeValues as N, type ARecordSchema as O, type PartialBy as P, isARecordSchema as Q, type Result as R, type SchemaValidator as S, isAObjectSchema as T, type InferObjectRawType as U, type ValidationContext as V, type WithAdapters as W, isObject as X, isARefSchema as Y, type ARefSchemaWithAdapters as Z, type ASchemaStrict as a, type ASchemaOptions as b, type ASchemaWithAdapters as c, type AArraySchema as d, type AScalarSchemaWithAdapters as e, type AObjectSchema as f, type ADiscriminatorSchemaWithAdapters as g, type ResolveObject as h, type AStringEnumSchemaWithAdapters as i, type AObjectSchemaOptions as j, type AObjectSchemaWithAdapters as k, type InferObjectOutput as l, type ARecordSchemaWithAdapters as m, type ARefSchema as n, type ValueError as o, type InferSubType as p, isValueError as q, errorMessageFromErrors as r, ValidationException as s, isValidationException as t, type ResultSuccess as u, type ResultFailure as v, newValidationContext as w, VALIDATOR_KEY as x, type SchemaMetadata as y, isASchema as z };
@@ -1,84 +1,80 @@
1
1
  import { Type } from '@arrirpc/type-defs';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
3
 
4
- declare function sanitizeJson(json: string): string;
5
- /**
6
- * Check if the input matches the specified schema. This is a type guard.
7
- */
8
- declare function validate<T = any>(schema: ASchema<T>, input: unknown): input is T;
9
- /**
10
- * Parse a JSON string or the result of JSON.parse(). Throws a ValidationError if parsing fails.
11
- */
12
- declare function parse<T = any>(schema: ASchema<T>, input: unknown): T;
13
- /**
14
- * Parse a value without throwing an error.
15
- */
16
- declare function safeParse<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
17
- /**
18
- * Try to convert input to match the specified schema. Throws a ValidationError if conversion fails.
19
- */
20
- declare function coerce<T = any>(schema: ASchema<T>, input: unknown): T;
21
- type SafeResult<T> = {
22
- success: true;
23
- value: T;
24
- } | {
25
- success: false;
26
- error: ValidationError;
27
- };
28
- /**
29
- * Convert a value into the specified schema without throwing an error.
30
- */
31
- declare function safeCoerce<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
32
- /**
33
- * Serialize a value into a JSON string
34
- */
35
- declare function serialize<T = any>(schema: ASchema<T>, input: T): string;
36
- declare function errors(schema: ASchema, input: unknown): ValueError[];
37
4
  interface ValueError {
5
+ /**
6
+ * Error message
7
+ */
8
+ message: string;
9
+ /**
10
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to where in the input is invalid
11
+ */
38
12
  instancePath: string;
39
- schemaPath: string;
40
- message?: string;
13
+ /**
14
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to the part of the schema that rejected the input
15
+ */
16
+ schemaPath?: string;
17
+ /**
18
+ * Arbitrary data that can be included with the error
19
+ */
41
20
  data?: any;
42
21
  }
43
- declare class ValidationError extends Error {
22
+ declare function isValueError(input: unknown): input is ValueError;
23
+ declare function errorMessageFromErrors(errors: ValueError[]): string;
24
+ declare class ValidationException extends Error {
44
25
  errors: ValueError[];
45
26
  constructor(options: {
46
- message: string;
27
+ message?: string;
47
28
  errors: ValueError[];
48
29
  });
49
30
  }
50
- declare function isValidationError(input: unknown): input is ValidationError;
31
+ declare function isValidationException(input: unknown): input is ValidationException;
32
+ type Result<T> = ResultSuccess<T> | ResultFailure;
33
+ type ResultSuccess<T> = {
34
+ success: true;
35
+ value: T;
36
+ };
37
+ type ResultFailure = {
38
+ success: false;
39
+ errors: ValueError[];
40
+ };
51
41
 
52
- declare const SCHEMA_METADATA: unique symbol;
53
42
  type MaybeNullable<T = any, TIsNullable extends boolean = false> = TIsNullable extends true ? T | null : T;
54
43
  interface ValidationContext {
55
44
  instancePath: string;
56
45
  schemaPath: string;
57
46
  errors: ValueError[];
47
+ exitOnFirstError: boolean;
48
+ depth: number;
49
+ maxDepth: number;
58
50
  discriminatorKey?: string;
59
51
  discriminatorValue?: string;
60
52
  }
53
+ declare function newValidationContext(exitOnFirstError?: boolean, maxDepth?: number): ValidationContext;
54
+ declare const VALIDATOR_KEY: " Symbol.for(arri/schema/validator/v1)";
61
55
  interface SchemaValidator<T> {
62
56
  output: T;
63
57
  optional?: boolean;
58
+ validate: (input: unknown) => input is T;
64
59
  parse: (input: unknown, context: ValidationContext) => T | undefined;
65
60
  coerce: (input: unknown, context: ValidationContext) => T | undefined;
66
- serialize: (input: T, context: ValidationContext) => string;
67
- validate: (input: unknown) => input is T;
68
- _isAdapted?: boolean;
61
+ serialize: (input: T, context: ValidationContext) => string | undefined;
69
62
  }
70
- interface SchemaMetadata<T> {
63
+ interface SchemaMetadata {
71
64
  [key: string]: any;
72
65
  id?: string;
73
66
  description?: string;
74
67
  isDeprecated?: boolean;
75
- [SCHEMA_METADATA]: SchemaValidator<T>;
76
68
  }
77
- interface ASchema<T = any> extends StandardSchemaV1<T> {
78
- metadata: SchemaMetadata<T>;
69
+ type WithAdapters<T = any> = StandardSchemaV1<T>;
70
+ interface ASchema<T = any> {
71
+ metadata?: SchemaMetadata;
79
72
  nullable?: boolean;
73
+ [VALIDATOR_KEY]: SchemaValidator<T>;
80
74
  }
75
+ type ASchemaStrict<TSchema extends ASchema> = Omit<TSchema, typeof VALIDATOR_KEY>;
81
76
  declare function isASchema(input: unknown): input is ASchema;
77
+ type ASchemaWithAdapters<T = any> = ASchema<T> & WithAdapters<T>;
82
78
  interface ASchemaOptions {
83
79
  id?: string;
84
80
  description?: string;
@@ -89,7 +85,7 @@ type ResolveObject<T> = Resolve<{
89
85
  [k in keyof T]: T[k];
90
86
  }>;
91
87
  type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
92
- type InferType<TInput extends ASchema<any>> = Resolve<TInput['metadata'][typeof SCHEMA_METADATA]['output']>;
88
+ type InferType<TInput extends ASchema<any>> = Resolve<TInput[typeof VALIDATOR_KEY]['output']>;
93
89
  /**
94
90
  * Infer a specific subtype of a discriminated union.
95
91
  * In order for this to work you must first infer the union type using `a.infer`
@@ -125,31 +121,37 @@ interface AScalarSchema<T extends Type | NumberType = any, TVal = any> extends A
125
121
  type: T;
126
122
  }
127
123
  declare function isAScalarSchema(input: unknown): input is AScalarSchema;
124
+ type AScalarSchemaWithAdapters<T extends Type | NumberType = any, TVal = any> = AScalarSchema<T, TVal> & WithAdapters<TVal>;
128
125
  declare const NumberTypeValues: readonly ["float32", "float64", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"];
129
126
  type NumberType = (typeof NumberTypeValues)[number];
130
127
  interface AArraySchema<TInnerSchema extends ASchema<any> = any> extends ASchema<Array<InferType<TInnerSchema>>> {
131
128
  elements: TInnerSchema;
132
129
  }
133
130
  declare function isAAraySchema(input: unknown): input is AArraySchema;
131
+ type AArraySchemaWithAdapters<TInnerSchema extends ASchema<any> = any> = AArraySchema<TInnerSchema> & WithAdapters<InferType<TInnerSchema>[]>;
134
132
  interface AStringEnumSchema<TValues extends string[]> extends ASchema<TValues[number]> {
135
133
  enum: TValues;
136
134
  }
137
135
  declare function isAStringEnumSchema(input: unknown): input is AStringEnumSchema<any>;
136
+ type AStringEnumSchemaWithAdapters<T extends string[]> = AStringEnumSchema<T> & WithAdapters<T[number]>;
138
137
  interface ADiscriminatorSchema<T> extends ASchema<T> {
139
138
  discriminator: string;
140
139
  mapping: Record<string, AObjectSchema<any>>;
141
140
  }
142
141
  declare function isADiscriminatorSchema(input: unknown): input is ADiscriminatorSchema<any>;
143
- interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boolean = false> extends ASchema<MaybeNullable<Record<string, InferType<TInnerSchema>>, TNullable>> {
142
+ type ADiscriminatorSchemaWithAdapters<T> = ADiscriminatorSchema<T> & WithAdapters<T>;
143
+ interface ARecordSchema<TInnerSchema extends ASchema<any>> extends ASchema<Record<string, InferType<TInnerSchema>>> {
144
144
  values: TInnerSchema;
145
145
  }
146
- declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
146
+ declare function isARecordSchema(input: unknown): input is ARecordSchema<any>;
147
+ type ARecordSchemaWithAdapters<TInnerSchema extends ASchema<any>> = ARecordSchema<TInnerSchema> & WithAdapters<Record<string, InferType<TInnerSchema>>>;
147
148
  interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
148
149
  properties: Record<string, ASchema>;
149
150
  optionalProperties?: Record<string, ASchema>;
150
151
  strict?: TStrict;
151
152
  }
152
153
  declare function isAObjectSchema(input: unknown): input is AObjectSchema;
154
+ type AObjectSchemaWithAdapters<T = any, TStrict extends boolean = false> = AObjectSchema<T, TStrict> & WithAdapters<T>;
153
155
  interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
154
156
  /**
155
157
  * Allow this object to include additional properties not specified here
@@ -158,12 +160,13 @@ interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends
158
160
  }
159
161
  type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
160
162
  type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
161
- [TKey in keyof TInput]: TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['optional'] extends true ? TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'] | undefined : TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'];
163
+ [TKey in keyof TInput]: TInput[TKey][typeof VALIDATOR_KEY]['optional'] extends true ? TInput[TKey][typeof VALIDATOR_KEY]['output'] | undefined : TInput[TKey][typeof VALIDATOR_KEY]['output'];
162
164
  } : never;
163
165
  declare function isObject(input: unknown): input is Record<any, any>;
164
166
  interface ARefSchema<T> extends ASchema<T> {
165
167
  ref: string;
166
168
  }
167
169
  declare function isARefSchema(input: unknown): input is ARefSchema<any>;
170
+ type ARefSchemaWithAdapters<T> = ARefSchema<T> & WithAdapters<T>;
168
171
 
169
- export { type ASchema as A, type Resolve as B, isAScalarSchema as C, type NumberType as D, isAAraySchema as E, isAStringEnumSchema as F, isADiscriminatorSchema as G, isARecordSchema as H, type InferType as I, isAObjectSchema as J, type InferObjectRawType as K, isObject as L, type MaybeNullable as M, NumberTypeValues as N, isARefSchema as O, type PartialBy as P, type ResolveObject as R, type SafeResult as S, type ValidationContext as V, type ASchemaOptions as a, type AArraySchema as b, type AScalarSchema as c, type AObjectSchema as d, type ADiscriminatorSchema as e, type AStringEnumSchema as f, type AObjectSchemaOptions as g, type InferObjectOutput as h, type ARecordSchema as i, type ARefSchema as j, coerce as k, errors as l, type InferSubType as m, safeParse as n, serialize as o, parse as p, sanitizeJson as q, type ValueError as r, safeCoerce as s, ValidationError as t, isValidationError as u, validate as v, SCHEMA_METADATA as w, type SchemaValidator as x, type SchemaMetadata as y, isASchema as z };
172
+ export { type ASchema as A, type Resolve as B, type AScalarSchema as C, isAScalarSchema as D, type NumberType as E, isAAraySchema as F, type AArraySchemaWithAdapters as G, type AStringEnumSchema as H, type InferType as I, isAStringEnumSchema as J, type ADiscriminatorSchema as K, isADiscriminatorSchema as L, type MaybeNullable as M, NumberTypeValues as N, type ARecordSchema as O, type PartialBy as P, isARecordSchema as Q, type Result as R, type SchemaValidator as S, isAObjectSchema as T, type InferObjectRawType as U, type ValidationContext as V, type WithAdapters as W, isObject as X, isARefSchema as Y, type ARefSchemaWithAdapters as Z, type ASchemaStrict as a, type ASchemaOptions as b, type ASchemaWithAdapters as c, type AArraySchema as d, type AScalarSchemaWithAdapters as e, type AObjectSchema as f, type ADiscriminatorSchemaWithAdapters as g, type ResolveObject as h, type AStringEnumSchemaWithAdapters as i, type AObjectSchemaOptions as j, type AObjectSchemaWithAdapters as k, type InferObjectOutput as l, type ARecordSchemaWithAdapters as m, type ARefSchema as n, type ValueError as o, type InferSubType as p, isValueError as q, errorMessageFromErrors as r, ValidationException as s, isValidationException as t, type ResultSuccess as u, type ResultFailure as v, newValidationContext as w, VALIDATOR_KEY as x, type SchemaMetadata as y, isASchema as z };
@@ -1,84 +1,80 @@
1
1
  import { Type } from '@arrirpc/type-defs';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
3
 
4
- declare function sanitizeJson(json: string): string;
5
- /**
6
- * Check if the input matches the specified schema. This is a type guard.
7
- */
8
- declare function validate<T = any>(schema: ASchema<T>, input: unknown): input is T;
9
- /**
10
- * Parse a JSON string or the result of JSON.parse(). Throws a ValidationError if parsing fails.
11
- */
12
- declare function parse<T = any>(schema: ASchema<T>, input: unknown): T;
13
- /**
14
- * Parse a value without throwing an error.
15
- */
16
- declare function safeParse<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
17
- /**
18
- * Try to convert input to match the specified schema. Throws a ValidationError if conversion fails.
19
- */
20
- declare function coerce<T = any>(schema: ASchema<T>, input: unknown): T;
21
- type SafeResult<T> = {
22
- success: true;
23
- value: T;
24
- } | {
25
- success: false;
26
- error: ValidationError;
27
- };
28
- /**
29
- * Convert a value into the specified schema without throwing an error.
30
- */
31
- declare function safeCoerce<T = any>(schema: ASchema<T>, input: unknown): SafeResult<T>;
32
- /**
33
- * Serialize a value into a JSON string
34
- */
35
- declare function serialize<T = any>(schema: ASchema<T>, input: T): string;
36
- declare function errors(schema: ASchema, input: unknown): ValueError[];
37
4
  interface ValueError {
5
+ /**
6
+ * Error message
7
+ */
8
+ message: string;
9
+ /**
10
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to where in the input is invalid
11
+ */
38
12
  instancePath: string;
39
- schemaPath: string;
40
- message?: string;
13
+ /**
14
+ * A JSON Pointer ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)) pointing to the part of the schema that rejected the input
15
+ */
16
+ schemaPath?: string;
17
+ /**
18
+ * Arbitrary data that can be included with the error
19
+ */
41
20
  data?: any;
42
21
  }
43
- declare class ValidationError extends Error {
22
+ declare function isValueError(input: unknown): input is ValueError;
23
+ declare function errorMessageFromErrors(errors: ValueError[]): string;
24
+ declare class ValidationException extends Error {
44
25
  errors: ValueError[];
45
26
  constructor(options: {
46
- message: string;
27
+ message?: string;
47
28
  errors: ValueError[];
48
29
  });
49
30
  }
50
- declare function isValidationError(input: unknown): input is ValidationError;
31
+ declare function isValidationException(input: unknown): input is ValidationException;
32
+ type Result<T> = ResultSuccess<T> | ResultFailure;
33
+ type ResultSuccess<T> = {
34
+ success: true;
35
+ value: T;
36
+ };
37
+ type ResultFailure = {
38
+ success: false;
39
+ errors: ValueError[];
40
+ };
51
41
 
52
- declare const SCHEMA_METADATA: unique symbol;
53
42
  type MaybeNullable<T = any, TIsNullable extends boolean = false> = TIsNullable extends true ? T | null : T;
54
43
  interface ValidationContext {
55
44
  instancePath: string;
56
45
  schemaPath: string;
57
46
  errors: ValueError[];
47
+ exitOnFirstError: boolean;
48
+ depth: number;
49
+ maxDepth: number;
58
50
  discriminatorKey?: string;
59
51
  discriminatorValue?: string;
60
52
  }
53
+ declare function newValidationContext(exitOnFirstError?: boolean, maxDepth?: number): ValidationContext;
54
+ declare const VALIDATOR_KEY: " Symbol.for(arri/schema/validator/v1)";
61
55
  interface SchemaValidator<T> {
62
56
  output: T;
63
57
  optional?: boolean;
58
+ validate: (input: unknown) => input is T;
64
59
  parse: (input: unknown, context: ValidationContext) => T | undefined;
65
60
  coerce: (input: unknown, context: ValidationContext) => T | undefined;
66
- serialize: (input: T, context: ValidationContext) => string;
67
- validate: (input: unknown) => input is T;
68
- _isAdapted?: boolean;
61
+ serialize: (input: T, context: ValidationContext) => string | undefined;
69
62
  }
70
- interface SchemaMetadata<T> {
63
+ interface SchemaMetadata {
71
64
  [key: string]: any;
72
65
  id?: string;
73
66
  description?: string;
74
67
  isDeprecated?: boolean;
75
- [SCHEMA_METADATA]: SchemaValidator<T>;
76
68
  }
77
- interface ASchema<T = any> extends StandardSchemaV1<T> {
78
- metadata: SchemaMetadata<T>;
69
+ type WithAdapters<T = any> = StandardSchemaV1<T>;
70
+ interface ASchema<T = any> {
71
+ metadata?: SchemaMetadata;
79
72
  nullable?: boolean;
73
+ [VALIDATOR_KEY]: SchemaValidator<T>;
80
74
  }
75
+ type ASchemaStrict<TSchema extends ASchema> = Omit<TSchema, typeof VALIDATOR_KEY>;
81
76
  declare function isASchema(input: unknown): input is ASchema;
77
+ type ASchemaWithAdapters<T = any> = ASchema<T> & WithAdapters<T>;
82
78
  interface ASchemaOptions {
83
79
  id?: string;
84
80
  description?: string;
@@ -89,7 +85,7 @@ type ResolveObject<T> = Resolve<{
89
85
  [k in keyof T]: T[k];
90
86
  }>;
91
87
  type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
92
- type InferType<TInput extends ASchema<any>> = Resolve<TInput['metadata'][typeof SCHEMA_METADATA]['output']>;
88
+ type InferType<TInput extends ASchema<any>> = Resolve<TInput[typeof VALIDATOR_KEY]['output']>;
93
89
  /**
94
90
  * Infer a specific subtype of a discriminated union.
95
91
  * In order for this to work you must first infer the union type using `a.infer`
@@ -125,31 +121,37 @@ interface AScalarSchema<T extends Type | NumberType = any, TVal = any> extends A
125
121
  type: T;
126
122
  }
127
123
  declare function isAScalarSchema(input: unknown): input is AScalarSchema;
124
+ type AScalarSchemaWithAdapters<T extends Type | NumberType = any, TVal = any> = AScalarSchema<T, TVal> & WithAdapters<TVal>;
128
125
  declare const NumberTypeValues: readonly ["float32", "float64", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"];
129
126
  type NumberType = (typeof NumberTypeValues)[number];
130
127
  interface AArraySchema<TInnerSchema extends ASchema<any> = any> extends ASchema<Array<InferType<TInnerSchema>>> {
131
128
  elements: TInnerSchema;
132
129
  }
133
130
  declare function isAAraySchema(input: unknown): input is AArraySchema;
131
+ type AArraySchemaWithAdapters<TInnerSchema extends ASchema<any> = any> = AArraySchema<TInnerSchema> & WithAdapters<InferType<TInnerSchema>[]>;
134
132
  interface AStringEnumSchema<TValues extends string[]> extends ASchema<TValues[number]> {
135
133
  enum: TValues;
136
134
  }
137
135
  declare function isAStringEnumSchema(input: unknown): input is AStringEnumSchema<any>;
136
+ type AStringEnumSchemaWithAdapters<T extends string[]> = AStringEnumSchema<T> & WithAdapters<T[number]>;
138
137
  interface ADiscriminatorSchema<T> extends ASchema<T> {
139
138
  discriminator: string;
140
139
  mapping: Record<string, AObjectSchema<any>>;
141
140
  }
142
141
  declare function isADiscriminatorSchema(input: unknown): input is ADiscriminatorSchema<any>;
143
- interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boolean = false> extends ASchema<MaybeNullable<Record<string, InferType<TInnerSchema>>, TNullable>> {
142
+ type ADiscriminatorSchemaWithAdapters<T> = ADiscriminatorSchema<T> & WithAdapters<T>;
143
+ interface ARecordSchema<TInnerSchema extends ASchema<any>> extends ASchema<Record<string, InferType<TInnerSchema>>> {
144
144
  values: TInnerSchema;
145
145
  }
146
- declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
146
+ declare function isARecordSchema(input: unknown): input is ARecordSchema<any>;
147
+ type ARecordSchemaWithAdapters<TInnerSchema extends ASchema<any>> = ARecordSchema<TInnerSchema> & WithAdapters<Record<string, InferType<TInnerSchema>>>;
147
148
  interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
148
149
  properties: Record<string, ASchema>;
149
150
  optionalProperties?: Record<string, ASchema>;
150
151
  strict?: TStrict;
151
152
  }
152
153
  declare function isAObjectSchema(input: unknown): input is AObjectSchema;
154
+ type AObjectSchemaWithAdapters<T = any, TStrict extends boolean = false> = AObjectSchema<T, TStrict> & WithAdapters<T>;
153
155
  interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
154
156
  /**
155
157
  * Allow this object to include additional properties not specified here
@@ -158,12 +160,13 @@ interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends
158
160
  }
159
161
  type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
160
162
  type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
161
- [TKey in keyof TInput]: TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['optional'] extends true ? TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'] | undefined : TInput[TKey]['metadata'][typeof SCHEMA_METADATA]['output'];
163
+ [TKey in keyof TInput]: TInput[TKey][typeof VALIDATOR_KEY]['optional'] extends true ? TInput[TKey][typeof VALIDATOR_KEY]['output'] | undefined : TInput[TKey][typeof VALIDATOR_KEY]['output'];
162
164
  } : never;
163
165
  declare function isObject(input: unknown): input is Record<any, any>;
164
166
  interface ARefSchema<T> extends ASchema<T> {
165
167
  ref: string;
166
168
  }
167
169
  declare function isARefSchema(input: unknown): input is ARefSchema<any>;
170
+ type ARefSchemaWithAdapters<T> = ARefSchema<T> & WithAdapters<T>;
168
171
 
169
- export { type ASchema as A, type Resolve as B, isAScalarSchema as C, type NumberType as D, isAAraySchema as E, isAStringEnumSchema as F, isADiscriminatorSchema as G, isARecordSchema as H, type InferType as I, isAObjectSchema as J, type InferObjectRawType as K, isObject as L, type MaybeNullable as M, NumberTypeValues as N, isARefSchema as O, type PartialBy as P, type ResolveObject as R, type SafeResult as S, type ValidationContext as V, type ASchemaOptions as a, type AArraySchema as b, type AScalarSchema as c, type AObjectSchema as d, type ADiscriminatorSchema as e, type AStringEnumSchema as f, type AObjectSchemaOptions as g, type InferObjectOutput as h, type ARecordSchema as i, type ARefSchema as j, coerce as k, errors as l, type InferSubType as m, safeParse as n, serialize as o, parse as p, sanitizeJson as q, type ValueError as r, safeCoerce as s, ValidationError as t, isValidationError as u, validate as v, SCHEMA_METADATA as w, type SchemaValidator as x, type SchemaMetadata as y, isASchema as z };
172
+ export { type ASchema as A, type Resolve as B, type AScalarSchema as C, isAScalarSchema as D, type NumberType as E, isAAraySchema as F, type AArraySchemaWithAdapters as G, type AStringEnumSchema as H, type InferType as I, isAStringEnumSchema as J, type ADiscriminatorSchema as K, isADiscriminatorSchema as L, type MaybeNullable as M, NumberTypeValues as N, type ARecordSchema as O, type PartialBy as P, isARecordSchema as Q, type Result as R, type SchemaValidator as S, isAObjectSchema as T, type InferObjectRawType as U, type ValidationContext as V, type WithAdapters as W, isObject as X, isARefSchema as Y, type ARefSchemaWithAdapters as Z, type ASchemaStrict as a, type ASchemaOptions as b, type ASchemaWithAdapters as c, type AArraySchema as d, type AScalarSchemaWithAdapters as e, type AObjectSchema as f, type ADiscriminatorSchemaWithAdapters as g, type ResolveObject as h, type AStringEnumSchemaWithAdapters as i, type AObjectSchemaOptions as j, type AObjectSchemaWithAdapters as k, type InferObjectOutput as l, type ARecordSchemaWithAdapters as m, type ARefSchema as n, type ValueError as o, type InferSubType as p, isValueError as q, errorMessageFromErrors as r, ValidationException as s, isValidationException as t, type ResultSuccess as u, type ResultFailure as v, newValidationContext as w, VALIDATOR_KEY as x, type SchemaMetadata as y, isASchema as z };