@arrirpc/schema 0.57.6 → 0.58.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.
- package/README.md +12 -14
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +7 -7
- package/dist/index.d.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.mjs +5 -5
- package/dist/shared/{schema.e933693f.cjs → schema.05b481fe.cjs} +30 -30
- package/dist/shared/{schema.8f2f6fef.mjs → schema.c92354cc.mjs} +30 -30
- package/dist/shared/{schema.16e4d5ba.d.cts → schema.cdac4a20.d.cts} +4 -4
- package/dist/shared/{schema.16e4d5ba.d.mts → schema.cdac4a20.d.mts} +4 -4
- package/dist/shared/{schema.16e4d5ba.d.ts → schema.cdac4a20.d.ts} +4 -4
- package/dist/testSuites.cjs +7 -12
- package/dist/testSuites.d.cts +1 -1
- package/dist/testSuites.d.mts +1 -1
- package/dist/testSuites.d.ts +1 -1
- package/dist/testSuites.mjs +7 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# Arri Schema
|
|
2
2
|
|
|
3
|
-
A type builder and validation library
|
|
4
|
-
|
|
5
|
-
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.
|
|
3
|
+
A type builder and validation library for [Arri Type Definitions](/specifications/arri_type_definition.md). 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.
|
|
6
4
|
|
|
7
5
|
## Project Philosophy
|
|
8
6
|
|
|
@@ -16,7 +14,11 @@ I am not looking to support every feature of Typescript's type system or even ev
|
|
|
16
14
|
|
|
17
15
|
### Adherence to RFC 8927
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
Originally this library was created as a way for building schemas for [Json Type Definition](https://jsontypedef.com/). However over time parts of the internal schema were modified to better suite the goals of Arri RPC. Some of these modifications include:
|
|
18
|
+
|
|
19
|
+
- Adding support for 64-bit integers
|
|
20
|
+
- Replacing the `additionalProperties` field with `strict` to allow for additional properties by default.
|
|
21
|
+
- Restrict `ref` to only be used for object types.
|
|
20
22
|
|
|
21
23
|
## Table of Contents
|
|
22
24
|
|
|
@@ -214,14 +216,13 @@ a.validate({
|
|
|
214
216
|
"created": {
|
|
215
217
|
"type": "timestamp"
|
|
216
218
|
}
|
|
217
|
-
}
|
|
218
|
-
"additionalProperties": true
|
|
219
|
+
}
|
|
219
220
|
}
|
|
220
221
|
```
|
|
221
222
|
|
|
222
223
|
#### Strict Mode
|
|
223
224
|
|
|
224
|
-
By default @arrirpc/schema 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 `
|
|
225
|
+
By default @arrirpc/schema 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 `strict` option.
|
|
225
226
|
|
|
226
227
|
```ts
|
|
227
228
|
const UserStrict = a.object(
|
|
@@ -231,7 +232,7 @@ const UserStrict = a.object(
|
|
|
231
232
|
created: a.timestamp(),
|
|
232
233
|
},
|
|
233
234
|
{
|
|
234
|
-
|
|
235
|
+
strict: true,
|
|
235
236
|
},
|
|
236
237
|
);
|
|
237
238
|
|
|
@@ -258,7 +259,7 @@ a.parse(UserStrict, {
|
|
|
258
259
|
"type": "timestamp"
|
|
259
260
|
}
|
|
260
261
|
},
|
|
261
|
-
"
|
|
262
|
+
"strict": true
|
|
262
263
|
}
|
|
263
264
|
```
|
|
264
265
|
|
|
@@ -339,16 +340,14 @@ a.validate(Shape, {
|
|
|
339
340
|
"height": {
|
|
340
341
|
"type": "float32"
|
|
341
342
|
}
|
|
342
|
-
}
|
|
343
|
-
"additionalProperties": true
|
|
343
|
+
}
|
|
344
344
|
},
|
|
345
345
|
"CIRCLE": {
|
|
346
346
|
"properties": {
|
|
347
347
|
"radius": {
|
|
348
348
|
"type": "float32"
|
|
349
349
|
}
|
|
350
|
-
}
|
|
351
|
-
"additionalProperties": true
|
|
350
|
+
}
|
|
352
351
|
}
|
|
353
352
|
}
|
|
354
353
|
}
|
|
@@ -426,7 +425,6 @@ a.validate(BinaryTree, {
|
|
|
426
425
|
"nullable": true
|
|
427
426
|
}
|
|
428
427
|
},
|
|
429
|
-
"additionalProperties": true,
|
|
430
428
|
"metadata": {
|
|
431
429
|
"id": "BinaryTree"
|
|
432
430
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const jtdUtils = require('jtd-utils');
|
|
4
4
|
const scule = require('scule');
|
|
5
|
-
const timestamp = require('./shared/schema.
|
|
5
|
+
const timestamp = require('./shared/schema.05b481fe.cjs');
|
|
6
6
|
|
|
7
7
|
function createParsingTemplate(input, schema) {
|
|
8
8
|
const fallbackTemplate = `
|
|
@@ -372,7 +372,7 @@ function objectTemplate$2(input) {
|
|
|
372
372
|
`${innerTargetVal}.${input.discriminatorKey} = "${input.discriminatorValue}";`
|
|
373
373
|
);
|
|
374
374
|
}
|
|
375
|
-
if (
|
|
375
|
+
if (input.schema.strict) {
|
|
376
376
|
const keyVal = `_${depthStr}_keys`;
|
|
377
377
|
const optionalKeyVal = `_${depthStr}_optionalKeys`;
|
|
378
378
|
const keys = Object.keys(input.schema.properties).map((key) => `"${key}"`).join(",");
|
|
@@ -382,7 +382,7 @@ function objectTemplate$2(input) {
|
|
|
382
382
|
const ${optionalKeyVal} = [${optionalKeys}];
|
|
383
383
|
for (let _key of Object.keys(${input.val})) {
|
|
384
384
|
if(!${keyVal}.includes(_key) && !${optionalKeyVal}.includes(_key) && _key !== "${input.discriminatorKey ?? ""}") {
|
|
385
|
-
$fallback("${input.instancePath}", "${input.schemaPath}/
|
|
385
|
+
$fallback("${input.instancePath}", "${input.schemaPath}/strict", \`The following key is now allowed by the schema "\${_key}".\`);
|
|
386
386
|
}
|
|
387
387
|
}
|
|
388
388
|
`
|
|
@@ -1194,7 +1194,7 @@ function objectTemplate(input) {
|
|
|
1194
1194
|
} else {
|
|
1195
1195
|
parts.push(`typeof ${input.val} === 'object' && ${input.val} !== null`);
|
|
1196
1196
|
}
|
|
1197
|
-
if (
|
|
1197
|
+
if (input.schema.strict) {
|
|
1198
1198
|
const allowedKeys = Object.keys(input.schema.properties);
|
|
1199
1199
|
for (const key of Object.keys(input.schema.optionalProperties ?? {})) {
|
|
1200
1200
|
allowedKeys.push(key);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.
|
|
2
|
-
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.
|
|
1
|
+
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.cdac4a20.cjs';
|
|
2
|
+
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.cdac4a20.cjs';
|
|
3
3
|
import { Schema } from 'jtd-utils';
|
|
4
4
|
|
|
5
5
|
declare function createParsingTemplate(input: string, schema: Schema): string;
|
|
@@ -187,8 +187,8 @@ declare function uint64(opts?: ASchemaOptions): AScalarSchema<"uint64", bigint>;
|
|
|
187
187
|
* a.validate(Schema, {foo: "", bar: 0}) // true
|
|
188
188
|
* a.validate(Schema, {foo: null, bar: 0}) // false
|
|
189
189
|
*/
|
|
190
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput
|
|
191
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput
|
|
190
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
191
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
192
192
|
declare function parseObjectSchema<T>(schema: AObjectSchema<T>, input: unknown, data: ValidationContext, coerce?: boolean): T | undefined;
|
|
193
193
|
declare function validateObjectSchema(schema: AObjectSchema, input: unknown): boolean;
|
|
194
194
|
/**
|
|
@@ -203,7 +203,7 @@ declare function validateObjectSchema(schema: AObjectSchema, input: unknown): bo
|
|
|
203
203
|
*
|
|
204
204
|
* const SubObject = a.pick(User, ['foo']) // { foo: string; }
|
|
205
205
|
*/
|
|
206
|
-
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
206
|
+
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Pick<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
207
207
|
/**
|
|
208
208
|
* Create an object schema by omitting keys from another object schema
|
|
209
209
|
*
|
|
@@ -216,9 +216,9 @@ declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys exten
|
|
|
216
216
|
*
|
|
217
217
|
* const SubObject = a.omit(BaseObject, ['foo']) // { bar: string; baz: string; }
|
|
218
218
|
*/
|
|
219
|
-
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
219
|
+
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Omit<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
220
220
|
declare function serializeObject(schema: AObjectSchema, input: any, data: ValidationContext): string;
|
|
221
|
-
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema,
|
|
221
|
+
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<ResolveObject<InferType<TBaseSchema> & InferType<TSchema>>, TAdditionalProps>;
|
|
222
222
|
declare function partial<TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(schema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Partial<InferType<TSchema>>, TAdditionalProps>;
|
|
223
223
|
|
|
224
224
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.
|
|
2
|
-
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.
|
|
1
|
+
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.cdac4a20.mjs';
|
|
2
|
+
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.cdac4a20.mjs';
|
|
3
3
|
import { Schema } from 'jtd-utils';
|
|
4
4
|
|
|
5
5
|
declare function createParsingTemplate(input: string, schema: Schema): string;
|
|
@@ -187,8 +187,8 @@ declare function uint64(opts?: ASchemaOptions): AScalarSchema<"uint64", bigint>;
|
|
|
187
187
|
* a.validate(Schema, {foo: "", bar: 0}) // true
|
|
188
188
|
* a.validate(Schema, {foo: null, bar: 0}) // false
|
|
189
189
|
*/
|
|
190
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput
|
|
191
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput
|
|
190
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
191
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
192
192
|
declare function parseObjectSchema<T>(schema: AObjectSchema<T>, input: unknown, data: ValidationContext, coerce?: boolean): T | undefined;
|
|
193
193
|
declare function validateObjectSchema(schema: AObjectSchema, input: unknown): boolean;
|
|
194
194
|
/**
|
|
@@ -203,7 +203,7 @@ declare function validateObjectSchema(schema: AObjectSchema, input: unknown): bo
|
|
|
203
203
|
*
|
|
204
204
|
* const SubObject = a.pick(User, ['foo']) // { foo: string; }
|
|
205
205
|
*/
|
|
206
|
-
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
206
|
+
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Pick<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
207
207
|
/**
|
|
208
208
|
* Create an object schema by omitting keys from another object schema
|
|
209
209
|
*
|
|
@@ -216,9 +216,9 @@ declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys exten
|
|
|
216
216
|
*
|
|
217
217
|
* const SubObject = a.omit(BaseObject, ['foo']) // { bar: string; baz: string; }
|
|
218
218
|
*/
|
|
219
|
-
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
219
|
+
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Omit<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
220
220
|
declare function serializeObject(schema: AObjectSchema, input: any, data: ValidationContext): string;
|
|
221
|
-
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema,
|
|
221
|
+
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<ResolveObject<InferType<TBaseSchema> & InferType<TSchema>>, TAdditionalProps>;
|
|
222
222
|
declare function partial<TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(schema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Partial<InferType<TSchema>>, TAdditionalProps>;
|
|
223
223
|
|
|
224
224
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.
|
|
2
|
-
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.
|
|
1
|
+
import { A as ASchema, I as InferType, S as SafeResult, a as ASchemaOptions, b as AArraySchema, c as AScalarSchema, d as AObjectSchema, e as ADiscriminatorSchema, R as ResolveObject, f as AStringEnumSchema, g as AObjectSchemaOptions, h as InferObjectOutput, V as ValidationContext, i as ARecordSchema, j as ARefSchema, k as coerce, l as errors, m as InferSubType, p as parse, s as safeCoerce, n as safeParse, o as serialize, v as validate } from './shared/schema.cdac4a20.js';
|
|
2
|
+
export { K as InferObjectRawType, M as MaybeNullable, D as NumberType, N as NumberTypeValues, P as PartialBy, B as Resolve, w as SCHEMA_METADATA, y as SchemaMetadata, x as SchemaValidator, t as ValidationError, r 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, u as isValidationError, q as sanitizeJson } from './shared/schema.cdac4a20.js';
|
|
3
3
|
import { Schema } from 'jtd-utils';
|
|
4
4
|
|
|
5
5
|
declare function createParsingTemplate(input: string, schema: Schema): string;
|
|
@@ -187,8 +187,8 @@ declare function uint64(opts?: ASchemaOptions): AScalarSchema<"uint64", bigint>;
|
|
|
187
187
|
* a.validate(Schema, {foo: "", bar: 0}) // true
|
|
188
188
|
* a.validate(Schema, {foo: null, bar: 0}) // false
|
|
189
189
|
*/
|
|
190
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput
|
|
191
|
-
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput
|
|
190
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(input: TInput, opts?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
191
|
+
declare function object<TInput extends Record<any, ASchema> = any, TAdditionalProps extends boolean = false>(id: string, input: TInput): AObjectSchema<InferObjectOutput<TInput>, TAdditionalProps>;
|
|
192
192
|
declare function parseObjectSchema<T>(schema: AObjectSchema<T>, input: unknown, data: ValidationContext, coerce?: boolean): T | undefined;
|
|
193
193
|
declare function validateObjectSchema(schema: AObjectSchema, input: unknown): boolean;
|
|
194
194
|
/**
|
|
@@ -203,7 +203,7 @@ declare function validateObjectSchema(schema: AObjectSchema, input: unknown): bo
|
|
|
203
203
|
*
|
|
204
204
|
* const SubObject = a.pick(User, ['foo']) // { foo: string; }
|
|
205
205
|
*/
|
|
206
|
-
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
206
|
+
declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Pick<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
207
207
|
/**
|
|
208
208
|
* Create an object schema by omitting keys from another object schema
|
|
209
209
|
*
|
|
@@ -216,9 +216,9 @@ declare function pick<TSchema extends AObjectSchema<any, any> = any, TKeys exten
|
|
|
216
216
|
*
|
|
217
217
|
* const SubObject = a.omit(BaseObject, ['foo']) // { bar: string; baz: string; }
|
|
218
218
|
*/
|
|
219
|
-
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[],
|
|
219
|
+
declare function omit<TSchema extends AObjectSchema<any, any> = any, TKeys extends keyof InferType<TSchema> = any, TAdditionalProps extends boolean = false>(inputSchema: TSchema, keys: TKeys[], options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Omit<InferType<TSchema>, TKeys>, TAdditionalProps>;
|
|
220
220
|
declare function serializeObject(schema: AObjectSchema, input: any, data: ValidationContext): string;
|
|
221
|
-
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema,
|
|
221
|
+
declare function extend<TBaseSchema extends AObjectSchema<any, any> = any, TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(baseSchema: TBaseSchema, inputSchema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<ResolveObject<InferType<TBaseSchema> & InferType<TSchema>>, TAdditionalProps>;
|
|
222
222
|
declare function partial<TSchema extends AObjectSchema<any, any> = any, TAdditionalProps extends boolean = false>(schema: TSchema, options?: AObjectSchemaOptions<TAdditionalProps>): AObjectSchema<Partial<InferType<TSchema>>, TAdditionalProps>;
|
|
223
223
|
|
|
224
224
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isSchemaFormProperties, isSchemaFormValues, isSchemaFormElements, isSchemaFormDiscriminator, isSchemaFormEmpty, isSchemaFormType, isSchemaFormEnum, isSchemaFormRef } from 'jtd-utils';
|
|
2
2
|
import { camelCase } from 'scule';
|
|
3
|
-
import { u as uint8Max, a as uint8Min, b as uint16Max, c as uint16Min, d as uint32Max, e as uint32Min, i as int8Max, f as int8Min, g as int16Max, h as int16Min, j as int32Max, k as int32Min, l as isAScalarSchema, m as isAObjectSchema, n as isAStringEnumSchema, o as isAAraySchema, p as isARecordSchema, q as isADiscriminatorSchema, r as isARefSchema, s as errors, V as ValidationError, t as any, v as array, w as boolean, x as clone, y as coerce, z as discriminator, A as enumerator, B as extend, C as float32, D as float64, E as int16, F as int32, G as int64, H as int8, I as nullable, J as number, K as object, L as omit, M as optional, N as parse, O as partial, P as pick, Q as record, R as recursive, S as safeCoerce, T as safeParse, U as serialize, W as string, X as stringEnum, Y as timestamp, Z as uint16, _ as uint32, $ as uint64, a0 as uint8, a1 as validate, a2 as SCHEMA_METADATA } from './shared/schema.
|
|
4
|
-
export { ae as NumberTypeValues, a4 as int64Max, a3 as int64Min, ad as isASchema, af as isObject, ac as isValidationError, a7 as parseObjectSchema, ab as sanitizeJson, a9 as serializeObject, aa as serializeSmallString, a6 as uint64Max, a5 as uint64Min, a8 as validateObjectSchema } from './shared/schema.
|
|
3
|
+
import { u as uint8Max, a as uint8Min, b as uint16Max, c as uint16Min, d as uint32Max, e as uint32Min, i as int8Max, f as int8Min, g as int16Max, h as int16Min, j as int32Max, k as int32Min, l as isAScalarSchema, m as isAObjectSchema, n as isAStringEnumSchema, o as isAAraySchema, p as isARecordSchema, q as isADiscriminatorSchema, r as isARefSchema, s as errors, V as ValidationError, t as any, v as array, w as boolean, x as clone, y as coerce, z as discriminator, A as enumerator, B as extend, C as float32, D as float64, E as int16, F as int32, G as int64, H as int8, I as nullable, J as number, K as object, L as omit, M as optional, N as parse, O as partial, P as pick, Q as record, R as recursive, S as safeCoerce, T as safeParse, U as serialize, W as string, X as stringEnum, Y as timestamp, Z as uint16, _ as uint32, $ as uint64, a0 as uint8, a1 as validate, a2 as SCHEMA_METADATA } from './shared/schema.c92354cc.mjs';
|
|
4
|
+
export { ae as NumberTypeValues, a4 as int64Max, a3 as int64Min, ad as isASchema, af as isObject, ac as isValidationError, a7 as parseObjectSchema, ab as sanitizeJson, a9 as serializeObject, aa as serializeSmallString, a6 as uint64Max, a5 as uint64Min, a8 as validateObjectSchema } from './shared/schema.c92354cc.mjs';
|
|
5
5
|
|
|
6
6
|
function createParsingTemplate(input, schema) {
|
|
7
7
|
const fallbackTemplate = `
|
|
@@ -371,7 +371,7 @@ function objectTemplate$2(input) {
|
|
|
371
371
|
`${innerTargetVal}.${input.discriminatorKey} = "${input.discriminatorValue}";`
|
|
372
372
|
);
|
|
373
373
|
}
|
|
374
|
-
if (
|
|
374
|
+
if (input.schema.strict) {
|
|
375
375
|
const keyVal = `_${depthStr}_keys`;
|
|
376
376
|
const optionalKeyVal = `_${depthStr}_optionalKeys`;
|
|
377
377
|
const keys = Object.keys(input.schema.properties).map((key) => `"${key}"`).join(",");
|
|
@@ -381,7 +381,7 @@ function objectTemplate$2(input) {
|
|
|
381
381
|
const ${optionalKeyVal} = [${optionalKeys}];
|
|
382
382
|
for (let _key of Object.keys(${input.val})) {
|
|
383
383
|
if(!${keyVal}.includes(_key) && !${optionalKeyVal}.includes(_key) && _key !== "${input.discriminatorKey ?? ""}") {
|
|
384
|
-
$fallback("${input.instancePath}", "${input.schemaPath}/
|
|
384
|
+
$fallback("${input.instancePath}", "${input.schemaPath}/strict", \`The following key is now allowed by the schema "\${_key}".\`);
|
|
385
385
|
}
|
|
386
386
|
}
|
|
387
387
|
`
|
|
@@ -1193,7 +1193,7 @@ function objectTemplate(input) {
|
|
|
1193
1193
|
} else {
|
|
1194
1194
|
parts.push(`typeof ${input.val} === 'object' && ${input.val} !== null`);
|
|
1195
1195
|
}
|
|
1196
|
-
if (
|
|
1196
|
+
if (input.schema.strict) {
|
|
1197
1197
|
const allowedKeys = Object.keys(input.schema.properties);
|
|
1198
1198
|
for (const key of Object.keys(input.schema.optionalProperties ?? {})) {
|
|
1199
1199
|
allowedKeys.push(key);
|
|
@@ -161,7 +161,7 @@ function parse$6(innerSchema, input, data, coerce = false) {
|
|
|
161
161
|
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
162
162
|
try {
|
|
163
163
|
parsedInput = JSON.parse(input);
|
|
164
|
-
} catch (
|
|
164
|
+
} catch (_) {
|
|
165
165
|
data.errors.push({
|
|
166
166
|
instancePath: data.instancePath,
|
|
167
167
|
schemaPath: `${data.schemaPath}/elements`,
|
|
@@ -842,7 +842,7 @@ function int64(opts = {}) {
|
|
|
842
842
|
instancePath: data.instancePath
|
|
843
843
|
});
|
|
844
844
|
return void 0;
|
|
845
|
-
} catch (
|
|
845
|
+
} catch (_) {
|
|
846
846
|
data.errors.push({
|
|
847
847
|
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt`,
|
|
848
848
|
schemaPath: `${data.schemaPath}/type`,
|
|
@@ -899,7 +899,7 @@ function uint64(opts = {}) {
|
|
|
899
899
|
instancePath: data.instancePath
|
|
900
900
|
});
|
|
901
901
|
return void 0;
|
|
902
|
-
} catch (
|
|
902
|
+
} catch (_) {
|
|
903
903
|
data.errors.push({
|
|
904
904
|
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt.`,
|
|
905
905
|
schemaPath: `${data.schemaPath}/type`,
|
|
@@ -1041,10 +1041,10 @@ function numberScalarType(type, opts, numTypeMatcher) {
|
|
|
1041
1041
|
function object(propA, propB) {
|
|
1042
1042
|
const isIdShorthand = typeof propA === "string";
|
|
1043
1043
|
const input = isIdShorthand ? propB : propA;
|
|
1044
|
-
const
|
|
1044
|
+
const options = isIdShorthand ? { id: propA } : propB ?? {};
|
|
1045
1045
|
const schema = {
|
|
1046
1046
|
properties: {},
|
|
1047
|
-
|
|
1047
|
+
strict: options.strict
|
|
1048
1048
|
};
|
|
1049
1049
|
for (const key of Object.keys(input)) {
|
|
1050
1050
|
const prop = input[key];
|
|
@@ -1060,9 +1060,9 @@ function object(propA, propB) {
|
|
|
1060
1060
|
const result = {
|
|
1061
1061
|
...schema,
|
|
1062
1062
|
metadata: {
|
|
1063
|
-
id:
|
|
1064
|
-
description:
|
|
1065
|
-
isDeprecated:
|
|
1063
|
+
id: options.id,
|
|
1064
|
+
description: options.description,
|
|
1065
|
+
isDeprecated: options.isDeprecated,
|
|
1066
1066
|
[SCHEMA_METADATA]: {
|
|
1067
1067
|
output: {},
|
|
1068
1068
|
parse(input2, context) {
|
|
@@ -1073,11 +1073,11 @@ function object(propA, propB) {
|
|
|
1073
1073
|
false
|
|
1074
1074
|
);
|
|
1075
1075
|
},
|
|
1076
|
-
coerce(input2,
|
|
1076
|
+
coerce(input2, context) {
|
|
1077
1077
|
return parseObjectSchema(
|
|
1078
1078
|
schema,
|
|
1079
1079
|
input2,
|
|
1080
|
-
|
|
1080
|
+
context,
|
|
1081
1081
|
true
|
|
1082
1082
|
);
|
|
1083
1083
|
},
|
|
@@ -1102,7 +1102,7 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1102
1102
|
try {
|
|
1103
1103
|
const result2 = JSON.parse(input);
|
|
1104
1104
|
parsedInput = result2;
|
|
1105
|
-
} catch (
|
|
1105
|
+
} catch (_err) {
|
|
1106
1106
|
data.errors.push({
|
|
1107
1107
|
instancePath: data.instancePath,
|
|
1108
1108
|
schemaPath: `${data.schemaPath}/properties`,
|
|
@@ -1124,13 +1124,13 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1124
1124
|
const inputKeys = Object.keys(parsedInput);
|
|
1125
1125
|
const requiredKeys = Object.keys(schema.properties);
|
|
1126
1126
|
const optionalKeys = Object.keys(optionalProps);
|
|
1127
|
-
if (
|
|
1127
|
+
if (schema.strict) {
|
|
1128
1128
|
for (const key of inputKeys) {
|
|
1129
1129
|
if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== data.discriminatorKey) {
|
|
1130
1130
|
data.errors.push({
|
|
1131
1131
|
instancePath: `${data.instancePath}/${key}`,
|
|
1132
|
-
schemaPath: `${data.schemaPath}/
|
|
1133
|
-
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set
|
|
1132
|
+
schemaPath: `${data.schemaPath}/strict`,
|
|
1133
|
+
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set strict to 'false'.`
|
|
1134
1134
|
});
|
|
1135
1135
|
}
|
|
1136
1136
|
}
|
|
@@ -1192,7 +1192,7 @@ function validateObjectSchema(schema, input) {
|
|
|
1192
1192
|
const optionalProps = schema.optionalProperties ?? {};
|
|
1193
1193
|
for (const key of Object.keys(input)) {
|
|
1194
1194
|
const prop = schema.properties[key] ?? optionalProps[key];
|
|
1195
|
-
if (!prop &&
|
|
1195
|
+
if (!prop && schema.strict) {
|
|
1196
1196
|
return false;
|
|
1197
1197
|
}
|
|
1198
1198
|
if (prop) {
|
|
@@ -1204,11 +1204,11 @@ function validateObjectSchema(schema, input) {
|
|
|
1204
1204
|
}
|
|
1205
1205
|
return true;
|
|
1206
1206
|
}
|
|
1207
|
-
function pick(inputSchema, keys,
|
|
1207
|
+
function pick(inputSchema, keys, options = {}) {
|
|
1208
1208
|
const schema = {
|
|
1209
1209
|
properties: {},
|
|
1210
1210
|
nullable: inputSchema.nullable,
|
|
1211
|
-
|
|
1211
|
+
strict: options.strict
|
|
1212
1212
|
};
|
|
1213
1213
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1214
1214
|
if (!schema.properties) {
|
|
@@ -1232,9 +1232,9 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1232
1232
|
return {
|
|
1233
1233
|
...schema,
|
|
1234
1234
|
metadata: {
|
|
1235
|
-
id:
|
|
1236
|
-
description:
|
|
1237
|
-
isDeprecated:
|
|
1235
|
+
id: options.id,
|
|
1236
|
+
description: options.description,
|
|
1237
|
+
isDeprecated: options.isDeprecated,
|
|
1238
1238
|
[SCHEMA_METADATA]: {
|
|
1239
1239
|
output: {},
|
|
1240
1240
|
parse: (input, context) => {
|
|
@@ -1263,13 +1263,13 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1263
1263
|
}
|
|
1264
1264
|
};
|
|
1265
1265
|
}
|
|
1266
|
-
function omit(inputSchema, keys,
|
|
1266
|
+
function omit(inputSchema, keys, options = {}) {
|
|
1267
1267
|
const schema = {
|
|
1268
1268
|
properties: {
|
|
1269
1269
|
...inputSchema.properties
|
|
1270
1270
|
},
|
|
1271
1271
|
nullable: inputSchema.nullable,
|
|
1272
|
-
|
|
1272
|
+
strict: options.strict
|
|
1273
1273
|
};
|
|
1274
1274
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1275
1275
|
if (keys.includes(key)) {
|
|
@@ -1292,9 +1292,9 @@ function omit(inputSchema, keys, opts = {}) {
|
|
|
1292
1292
|
return {
|
|
1293
1293
|
...schema,
|
|
1294
1294
|
metadata: {
|
|
1295
|
-
id:
|
|
1296
|
-
description:
|
|
1297
|
-
isDeprecated:
|
|
1295
|
+
id: options.id,
|
|
1296
|
+
description: options.description,
|
|
1297
|
+
isDeprecated: options.isDeprecated,
|
|
1298
1298
|
[SCHEMA_METADATA]: {
|
|
1299
1299
|
output: {},
|
|
1300
1300
|
validate(input) {
|
|
@@ -1355,7 +1355,7 @@ function serializeObject(schema, input, data) {
|
|
|
1355
1355
|
}
|
|
1356
1356
|
return `{${strParts.join(",")}}`;
|
|
1357
1357
|
}
|
|
1358
|
-
function extend(baseSchema, inputSchema,
|
|
1358
|
+
function extend(baseSchema, inputSchema, options = {}) {
|
|
1359
1359
|
const schema = {
|
|
1360
1360
|
properties: {
|
|
1361
1361
|
...baseSchema.properties,
|
|
@@ -1365,12 +1365,12 @@ function extend(baseSchema, inputSchema, opts = {}) {
|
|
|
1365
1365
|
...baseSchema.optionalProperties,
|
|
1366
1366
|
...inputSchema.optionalProperties
|
|
1367
1367
|
},
|
|
1368
|
-
|
|
1368
|
+
strict: options.strict
|
|
1369
1369
|
};
|
|
1370
1370
|
const isType = (input) => validateObjectSchema(schema, input);
|
|
1371
1371
|
const meta = {
|
|
1372
|
-
id:
|
|
1373
|
-
description:
|
|
1372
|
+
id: options.id,
|
|
1373
|
+
description: options.description,
|
|
1374
1374
|
[SCHEMA_METADATA]: {
|
|
1375
1375
|
output: {},
|
|
1376
1376
|
parse(input, context) {
|
|
@@ -1392,7 +1392,7 @@ function partial(schema, options = {}) {
|
|
|
1392
1392
|
const newSchema = {
|
|
1393
1393
|
properties: {},
|
|
1394
1394
|
optionalProperties: {},
|
|
1395
|
-
|
|
1395
|
+
strict: options.strict,
|
|
1396
1396
|
nullable: schema.nullable
|
|
1397
1397
|
};
|
|
1398
1398
|
for (const key of Object.keys(schema.properties)) {
|
|
@@ -159,7 +159,7 @@ function parse$6(innerSchema, input, data, coerce = false) {
|
|
|
159
159
|
if (data.instancePath.length === 0 && typeof input === "string") {
|
|
160
160
|
try {
|
|
161
161
|
parsedInput = JSON.parse(input);
|
|
162
|
-
} catch (
|
|
162
|
+
} catch (_) {
|
|
163
163
|
data.errors.push({
|
|
164
164
|
instancePath: data.instancePath,
|
|
165
165
|
schemaPath: `${data.schemaPath}/elements`,
|
|
@@ -840,7 +840,7 @@ function int64(opts = {}) {
|
|
|
840
840
|
instancePath: data.instancePath
|
|
841
841
|
});
|
|
842
842
|
return void 0;
|
|
843
|
-
} catch (
|
|
843
|
+
} catch (_) {
|
|
844
844
|
data.errors.push({
|
|
845
845
|
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt`,
|
|
846
846
|
schemaPath: `${data.schemaPath}/type`,
|
|
@@ -897,7 +897,7 @@ function uint64(opts = {}) {
|
|
|
897
897
|
instancePath: data.instancePath
|
|
898
898
|
});
|
|
899
899
|
return void 0;
|
|
900
|
-
} catch (
|
|
900
|
+
} catch (_) {
|
|
901
901
|
data.errors.push({
|
|
902
902
|
message: `Error at ${data.instancePath}. Unable to transform ${input} to BigInt.`,
|
|
903
903
|
schemaPath: `${data.schemaPath}/type`,
|
|
@@ -1039,10 +1039,10 @@ function numberScalarType(type, opts, numTypeMatcher) {
|
|
|
1039
1039
|
function object(propA, propB) {
|
|
1040
1040
|
const isIdShorthand = typeof propA === "string";
|
|
1041
1041
|
const input = isIdShorthand ? propB : propA;
|
|
1042
|
-
const
|
|
1042
|
+
const options = isIdShorthand ? { id: propA } : propB ?? {};
|
|
1043
1043
|
const schema = {
|
|
1044
1044
|
properties: {},
|
|
1045
|
-
|
|
1045
|
+
strict: options.strict
|
|
1046
1046
|
};
|
|
1047
1047
|
for (const key of Object.keys(input)) {
|
|
1048
1048
|
const prop = input[key];
|
|
@@ -1058,9 +1058,9 @@ function object(propA, propB) {
|
|
|
1058
1058
|
const result = {
|
|
1059
1059
|
...schema,
|
|
1060
1060
|
metadata: {
|
|
1061
|
-
id:
|
|
1062
|
-
description:
|
|
1063
|
-
isDeprecated:
|
|
1061
|
+
id: options.id,
|
|
1062
|
+
description: options.description,
|
|
1063
|
+
isDeprecated: options.isDeprecated,
|
|
1064
1064
|
[SCHEMA_METADATA]: {
|
|
1065
1065
|
output: {},
|
|
1066
1066
|
parse(input2, context) {
|
|
@@ -1071,11 +1071,11 @@ function object(propA, propB) {
|
|
|
1071
1071
|
false
|
|
1072
1072
|
);
|
|
1073
1073
|
},
|
|
1074
|
-
coerce(input2,
|
|
1074
|
+
coerce(input2, context) {
|
|
1075
1075
|
return parseObjectSchema(
|
|
1076
1076
|
schema,
|
|
1077
1077
|
input2,
|
|
1078
|
-
|
|
1078
|
+
context,
|
|
1079
1079
|
true
|
|
1080
1080
|
);
|
|
1081
1081
|
},
|
|
@@ -1100,7 +1100,7 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1100
1100
|
try {
|
|
1101
1101
|
const result2 = JSON.parse(input);
|
|
1102
1102
|
parsedInput = result2;
|
|
1103
|
-
} catch (
|
|
1103
|
+
} catch (_err) {
|
|
1104
1104
|
data.errors.push({
|
|
1105
1105
|
instancePath: data.instancePath,
|
|
1106
1106
|
schemaPath: `${data.schemaPath}/properties`,
|
|
@@ -1122,13 +1122,13 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1122
1122
|
const inputKeys = Object.keys(parsedInput);
|
|
1123
1123
|
const requiredKeys = Object.keys(schema.properties);
|
|
1124
1124
|
const optionalKeys = Object.keys(optionalProps);
|
|
1125
|
-
if (
|
|
1125
|
+
if (schema.strict) {
|
|
1126
1126
|
for (const key of inputKeys) {
|
|
1127
1127
|
if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== data.discriminatorKey) {
|
|
1128
1128
|
data.errors.push({
|
|
1129
1129
|
instancePath: `${data.instancePath}/${key}`,
|
|
1130
|
-
schemaPath: `${data.schemaPath}/
|
|
1131
|
-
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set
|
|
1130
|
+
schemaPath: `${data.schemaPath}/strict`,
|
|
1131
|
+
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set strict to 'false'.`
|
|
1132
1132
|
});
|
|
1133
1133
|
}
|
|
1134
1134
|
}
|
|
@@ -1190,7 +1190,7 @@ function validateObjectSchema(schema, input) {
|
|
|
1190
1190
|
const optionalProps = schema.optionalProperties ?? {};
|
|
1191
1191
|
for (const key of Object.keys(input)) {
|
|
1192
1192
|
const prop = schema.properties[key] ?? optionalProps[key];
|
|
1193
|
-
if (!prop &&
|
|
1193
|
+
if (!prop && schema.strict) {
|
|
1194
1194
|
return false;
|
|
1195
1195
|
}
|
|
1196
1196
|
if (prop) {
|
|
@@ -1202,11 +1202,11 @@ function validateObjectSchema(schema, input) {
|
|
|
1202
1202
|
}
|
|
1203
1203
|
return true;
|
|
1204
1204
|
}
|
|
1205
|
-
function pick(inputSchema, keys,
|
|
1205
|
+
function pick(inputSchema, keys, options = {}) {
|
|
1206
1206
|
const schema = {
|
|
1207
1207
|
properties: {},
|
|
1208
1208
|
nullable: inputSchema.nullable,
|
|
1209
|
-
|
|
1209
|
+
strict: options.strict
|
|
1210
1210
|
};
|
|
1211
1211
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1212
1212
|
if (!schema.properties) {
|
|
@@ -1230,9 +1230,9 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1230
1230
|
return {
|
|
1231
1231
|
...schema,
|
|
1232
1232
|
metadata: {
|
|
1233
|
-
id:
|
|
1234
|
-
description:
|
|
1235
|
-
isDeprecated:
|
|
1233
|
+
id: options.id,
|
|
1234
|
+
description: options.description,
|
|
1235
|
+
isDeprecated: options.isDeprecated,
|
|
1236
1236
|
[SCHEMA_METADATA]: {
|
|
1237
1237
|
output: {},
|
|
1238
1238
|
parse: (input, context) => {
|
|
@@ -1261,13 +1261,13 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1261
1261
|
}
|
|
1262
1262
|
};
|
|
1263
1263
|
}
|
|
1264
|
-
function omit(inputSchema, keys,
|
|
1264
|
+
function omit(inputSchema, keys, options = {}) {
|
|
1265
1265
|
const schema = {
|
|
1266
1266
|
properties: {
|
|
1267
1267
|
...inputSchema.properties
|
|
1268
1268
|
},
|
|
1269
1269
|
nullable: inputSchema.nullable,
|
|
1270
|
-
|
|
1270
|
+
strict: options.strict
|
|
1271
1271
|
};
|
|
1272
1272
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1273
1273
|
if (keys.includes(key)) {
|
|
@@ -1290,9 +1290,9 @@ function omit(inputSchema, keys, opts = {}) {
|
|
|
1290
1290
|
return {
|
|
1291
1291
|
...schema,
|
|
1292
1292
|
metadata: {
|
|
1293
|
-
id:
|
|
1294
|
-
description:
|
|
1295
|
-
isDeprecated:
|
|
1293
|
+
id: options.id,
|
|
1294
|
+
description: options.description,
|
|
1295
|
+
isDeprecated: options.isDeprecated,
|
|
1296
1296
|
[SCHEMA_METADATA]: {
|
|
1297
1297
|
output: {},
|
|
1298
1298
|
validate(input) {
|
|
@@ -1353,7 +1353,7 @@ function serializeObject(schema, input, data) {
|
|
|
1353
1353
|
}
|
|
1354
1354
|
return `{${strParts.join(",")}}`;
|
|
1355
1355
|
}
|
|
1356
|
-
function extend(baseSchema, inputSchema,
|
|
1356
|
+
function extend(baseSchema, inputSchema, options = {}) {
|
|
1357
1357
|
const schema = {
|
|
1358
1358
|
properties: {
|
|
1359
1359
|
...baseSchema.properties,
|
|
@@ -1363,12 +1363,12 @@ function extend(baseSchema, inputSchema, opts = {}) {
|
|
|
1363
1363
|
...baseSchema.optionalProperties,
|
|
1364
1364
|
...inputSchema.optionalProperties
|
|
1365
1365
|
},
|
|
1366
|
-
|
|
1366
|
+
strict: options.strict
|
|
1367
1367
|
};
|
|
1368
1368
|
const isType = (input) => validateObjectSchema(schema, input);
|
|
1369
1369
|
const meta = {
|
|
1370
|
-
id:
|
|
1371
|
-
description:
|
|
1370
|
+
id: options.id,
|
|
1371
|
+
description: options.description,
|
|
1372
1372
|
[SCHEMA_METADATA]: {
|
|
1373
1373
|
output: {},
|
|
1374
1374
|
parse(input, context) {
|
|
@@ -1390,7 +1390,7 @@ function partial(schema, options = {}) {
|
|
|
1390
1390
|
const newSchema = {
|
|
1391
1391
|
properties: {},
|
|
1392
1392
|
optionalProperties: {},
|
|
1393
|
-
|
|
1393
|
+
strict: options.strict,
|
|
1394
1394
|
nullable: schema.nullable
|
|
1395
1395
|
};
|
|
1396
1396
|
for (const key of Object.keys(schema.properties)) {
|
|
@@ -143,19 +143,19 @@ interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boo
|
|
|
143
143
|
values: TInnerSchema;
|
|
144
144
|
}
|
|
145
145
|
declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
|
|
146
|
-
interface AObjectSchema<TVal = any,
|
|
146
|
+
interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
|
|
147
147
|
properties: Record<string, ASchema>;
|
|
148
148
|
optionalProperties?: Record<string, ASchema>;
|
|
149
|
-
|
|
149
|
+
strict?: TStrict;
|
|
150
150
|
}
|
|
151
151
|
declare function isAObjectSchema(input: unknown): input is AObjectSchema;
|
|
152
152
|
interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
|
|
153
153
|
/**
|
|
154
154
|
* Allow this object to include additional properties not specified here
|
|
155
155
|
*/
|
|
156
|
-
|
|
156
|
+
strict?: TAdditionalProps;
|
|
157
157
|
}
|
|
158
|
-
type InferObjectOutput<TInput = any
|
|
158
|
+
type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
|
|
159
159
|
type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
|
|
160
160
|
[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"];
|
|
161
161
|
} : never;
|
|
@@ -143,19 +143,19 @@ interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boo
|
|
|
143
143
|
values: TInnerSchema;
|
|
144
144
|
}
|
|
145
145
|
declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
|
|
146
|
-
interface AObjectSchema<TVal = any,
|
|
146
|
+
interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
|
|
147
147
|
properties: Record<string, ASchema>;
|
|
148
148
|
optionalProperties?: Record<string, ASchema>;
|
|
149
|
-
|
|
149
|
+
strict?: TStrict;
|
|
150
150
|
}
|
|
151
151
|
declare function isAObjectSchema(input: unknown): input is AObjectSchema;
|
|
152
152
|
interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
|
|
153
153
|
/**
|
|
154
154
|
* Allow this object to include additional properties not specified here
|
|
155
155
|
*/
|
|
156
|
-
|
|
156
|
+
strict?: TAdditionalProps;
|
|
157
157
|
}
|
|
158
|
-
type InferObjectOutput<TInput = any
|
|
158
|
+
type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
|
|
159
159
|
type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
|
|
160
160
|
[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"];
|
|
161
161
|
} : never;
|
|
@@ -143,19 +143,19 @@ interface ARecordSchema<TInnerSchema extends ASchema<any>, TNullable extends boo
|
|
|
143
143
|
values: TInnerSchema;
|
|
144
144
|
}
|
|
145
145
|
declare function isARecordSchema(input: unknown): input is ARecordSchema<any, any>;
|
|
146
|
-
interface AObjectSchema<TVal = any,
|
|
146
|
+
interface AObjectSchema<TVal = any, TStrict extends boolean = false> extends ASchema<TVal> {
|
|
147
147
|
properties: Record<string, ASchema>;
|
|
148
148
|
optionalProperties?: Record<string, ASchema>;
|
|
149
|
-
|
|
149
|
+
strict?: TStrict;
|
|
150
150
|
}
|
|
151
151
|
declare function isAObjectSchema(input: unknown): input is AObjectSchema;
|
|
152
152
|
interface AObjectSchemaOptions<TAdditionalProps extends boolean = false> extends ASchemaOptions {
|
|
153
153
|
/**
|
|
154
154
|
* Allow this object to include additional properties not specified here
|
|
155
155
|
*/
|
|
156
|
-
|
|
156
|
+
strict?: TAdditionalProps;
|
|
157
157
|
}
|
|
158
|
-
type InferObjectOutput<TInput = any
|
|
158
|
+
type InferObjectOutput<TInput = any> = ResolveObject<InferObjectRawType<TInput>>;
|
|
159
159
|
type InferObjectRawType<TInput> = TInput extends Record<any, any> ? {
|
|
160
160
|
[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"];
|
|
161
161
|
} : never;
|
package/dist/testSuites.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require('jtd-utils');
|
|
4
4
|
require('scule');
|
|
5
|
-
const timestamp = require('./shared/schema.
|
|
5
|
+
const timestamp = require('./shared/schema.05b481fe.cjs');
|
|
6
6
|
|
|
7
7
|
const User = timestamp.object({
|
|
8
8
|
id: timestamp.string(),
|
|
@@ -157,17 +157,12 @@ world`,
|
|
|
157
157
|
int32: {
|
|
158
158
|
schema: timestamp.int32(),
|
|
159
159
|
goodInputs: [491451, -13411],
|
|
160
|
-
badInputs: [
|
|
161
|
-
1e18,
|
|
162
|
-
-1e19,
|
|
163
|
-
199.5,
|
|
164
|
-
"hello world"
|
|
165
|
-
]
|
|
160
|
+
badInputs: [999999999999999, -999999999999999, 199.5, "hello world"]
|
|
166
161
|
},
|
|
167
162
|
uint32: {
|
|
168
163
|
schema: timestamp.uint32(),
|
|
169
164
|
goodInputs: [4815141, 100],
|
|
170
|
-
badInputs: [-1, 100.5,
|
|
165
|
+
badInputs: [-1, 100.5, 1399999999999999, "hello world"]
|
|
171
166
|
},
|
|
172
167
|
int16: {
|
|
173
168
|
schema: timestamp.int16(),
|
|
@@ -662,13 +657,13 @@ world`,
|
|
|
662
657
|
{ id: "1" }
|
|
663
658
|
]
|
|
664
659
|
},
|
|
665
|
-
"object with
|
|
660
|
+
"object with strict true": {
|
|
666
661
|
schema: timestamp.object(
|
|
667
662
|
{
|
|
668
663
|
id: timestamp.string(),
|
|
669
664
|
name: timestamp.string()
|
|
670
665
|
},
|
|
671
|
-
{
|
|
666
|
+
{ strict: true }
|
|
672
667
|
),
|
|
673
668
|
goodInputs: [
|
|
674
669
|
{
|
|
@@ -692,13 +687,13 @@ world`,
|
|
|
692
687
|
true
|
|
693
688
|
]
|
|
694
689
|
},
|
|
695
|
-
"object with
|
|
690
|
+
"object with strict false": {
|
|
696
691
|
schema: timestamp.object(
|
|
697
692
|
{
|
|
698
693
|
id: timestamp.string(),
|
|
699
694
|
name: timestamp.string()
|
|
700
695
|
},
|
|
701
|
-
{
|
|
696
|
+
{ strict: false }
|
|
702
697
|
),
|
|
703
698
|
goodInputs: [
|
|
704
699
|
{
|
package/dist/testSuites.d.cts
CHANGED
package/dist/testSuites.d.mts
CHANGED
package/dist/testSuites.d.ts
CHANGED
package/dist/testSuites.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'jtd-utils';
|
|
2
2
|
import 'scule';
|
|
3
|
-
import { K as object, W as string, M as optional, I as nullable, F as int32, z as discriminator, w as boolean, X as stringEnum, v as array, Y as timestamp, _ as uint32, t as any, J as number, Q as record, R as recursive, D as float64, C as float32, G as int64, E as int16, Z as uint16, H as int8, a0 as uint8, O as partial, $ as uint64, P as pick, A as enumerator } from './shared/schema.
|
|
3
|
+
import { K as object, W as string, M as optional, I as nullable, F as int32, z as discriminator, w as boolean, X as stringEnum, v as array, Y as timestamp, _ as uint32, t as any, J as number, Q as record, R as recursive, D as float64, C as float32, G as int64, E as int16, Z as uint16, H as int8, a0 as uint8, O as partial, $ as uint64, P as pick, A as enumerator } from './shared/schema.c92354cc.mjs';
|
|
4
4
|
|
|
5
5
|
const User = object({
|
|
6
6
|
id: string(),
|
|
@@ -155,17 +155,12 @@ world`,
|
|
|
155
155
|
int32: {
|
|
156
156
|
schema: int32(),
|
|
157
157
|
goodInputs: [491451, -13411],
|
|
158
|
-
badInputs: [
|
|
159
|
-
1e18,
|
|
160
|
-
-1e19,
|
|
161
|
-
199.5,
|
|
162
|
-
"hello world"
|
|
163
|
-
]
|
|
158
|
+
badInputs: [999999999999999, -999999999999999, 199.5, "hello world"]
|
|
164
159
|
},
|
|
165
160
|
uint32: {
|
|
166
161
|
schema: uint32(),
|
|
167
162
|
goodInputs: [4815141, 100],
|
|
168
|
-
badInputs: [-1, 100.5,
|
|
163
|
+
badInputs: [-1, 100.5, 1399999999999999, "hello world"]
|
|
169
164
|
},
|
|
170
165
|
int16: {
|
|
171
166
|
schema: int16(),
|
|
@@ -660,13 +655,13 @@ world`,
|
|
|
660
655
|
{ id: "1" }
|
|
661
656
|
]
|
|
662
657
|
},
|
|
663
|
-
"object with
|
|
658
|
+
"object with strict true": {
|
|
664
659
|
schema: object(
|
|
665
660
|
{
|
|
666
661
|
id: string(),
|
|
667
662
|
name: string()
|
|
668
663
|
},
|
|
669
|
-
{
|
|
664
|
+
{ strict: true }
|
|
670
665
|
),
|
|
671
666
|
goodInputs: [
|
|
672
667
|
{
|
|
@@ -690,13 +685,13 @@ world`,
|
|
|
690
685
|
true
|
|
691
686
|
]
|
|
692
687
|
},
|
|
693
|
-
"object with
|
|
688
|
+
"object with strict false": {
|
|
694
689
|
schema: object(
|
|
695
690
|
{
|
|
696
691
|
id: string(),
|
|
697
692
|
name: string()
|
|
698
693
|
},
|
|
699
|
-
{
|
|
694
|
+
{ strict: false }
|
|
700
695
|
),
|
|
701
696
|
goodInputs: [
|
|
702
697
|
{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arrirpc/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.58.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"scule": "^1.3.0",
|
|
26
26
|
"uncrypto": "^0.1.3",
|
|
27
|
-
"jtd-utils": "0.
|
|
27
|
+
"jtd-utils": "0.58.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@faker-js/faker": "^8.4.1",
|