@arrirpc/schema 0.57.8 → 0.58.1
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.8f2f6fef.mjs → schema.31273dd5.mjs} +44 -34
- package/dist/shared/{schema.e933693f.cjs → schema.78a649a7.cjs} +44 -34
- 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.78a649a7.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.31273dd5.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.31273dd5.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);
|
|
@@ -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,11 +1039,13 @@ 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
|
-
properties: {}
|
|
1045
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1044
|
+
properties: {}
|
|
1046
1045
|
};
|
|
1046
|
+
if (typeof options.strict === "boolean") {
|
|
1047
|
+
schema.strict = options.strict;
|
|
1048
|
+
}
|
|
1047
1049
|
for (const key of Object.keys(input)) {
|
|
1048
1050
|
const prop = input[key];
|
|
1049
1051
|
if (prop.metadata[SCHEMA_METADATA].optional) {
|
|
@@ -1058,9 +1060,9 @@ function object(propA, propB) {
|
|
|
1058
1060
|
const result = {
|
|
1059
1061
|
...schema,
|
|
1060
1062
|
metadata: {
|
|
1061
|
-
id:
|
|
1062
|
-
description:
|
|
1063
|
-
isDeprecated:
|
|
1063
|
+
id: options.id,
|
|
1064
|
+
description: options.description,
|
|
1065
|
+
isDeprecated: options.isDeprecated,
|
|
1064
1066
|
[SCHEMA_METADATA]: {
|
|
1065
1067
|
output: {},
|
|
1066
1068
|
parse(input2, context) {
|
|
@@ -1071,11 +1073,11 @@ function object(propA, propB) {
|
|
|
1071
1073
|
false
|
|
1072
1074
|
);
|
|
1073
1075
|
},
|
|
1074
|
-
coerce(input2,
|
|
1076
|
+
coerce(input2, context) {
|
|
1075
1077
|
return parseObjectSchema(
|
|
1076
1078
|
schema,
|
|
1077
1079
|
input2,
|
|
1078
|
-
|
|
1080
|
+
context,
|
|
1079
1081
|
true
|
|
1080
1082
|
);
|
|
1081
1083
|
},
|
|
@@ -1100,7 +1102,7 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1100
1102
|
try {
|
|
1101
1103
|
const result2 = JSON.parse(input);
|
|
1102
1104
|
parsedInput = result2;
|
|
1103
|
-
} catch (
|
|
1105
|
+
} catch (_err) {
|
|
1104
1106
|
data.errors.push({
|
|
1105
1107
|
instancePath: data.instancePath,
|
|
1106
1108
|
schemaPath: `${data.schemaPath}/properties`,
|
|
@@ -1122,13 +1124,13 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1122
1124
|
const inputKeys = Object.keys(parsedInput);
|
|
1123
1125
|
const requiredKeys = Object.keys(schema.properties);
|
|
1124
1126
|
const optionalKeys = Object.keys(optionalProps);
|
|
1125
|
-
if (
|
|
1127
|
+
if (schema.strict) {
|
|
1126
1128
|
for (const key of inputKeys) {
|
|
1127
1129
|
if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== data.discriminatorKey) {
|
|
1128
1130
|
data.errors.push({
|
|
1129
1131
|
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
|
|
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'.`
|
|
1132
1134
|
});
|
|
1133
1135
|
}
|
|
1134
1136
|
}
|
|
@@ -1190,7 +1192,7 @@ function validateObjectSchema(schema, input) {
|
|
|
1190
1192
|
const optionalProps = schema.optionalProperties ?? {};
|
|
1191
1193
|
for (const key of Object.keys(input)) {
|
|
1192
1194
|
const prop = schema.properties[key] ?? optionalProps[key];
|
|
1193
|
-
if (!prop &&
|
|
1195
|
+
if (!prop && schema.strict) {
|
|
1194
1196
|
return false;
|
|
1195
1197
|
}
|
|
1196
1198
|
if (prop) {
|
|
@@ -1202,12 +1204,14 @@ function validateObjectSchema(schema, input) {
|
|
|
1202
1204
|
}
|
|
1203
1205
|
return true;
|
|
1204
1206
|
}
|
|
1205
|
-
function pick(inputSchema, keys,
|
|
1207
|
+
function pick(inputSchema, keys, options = {}) {
|
|
1206
1208
|
const schema = {
|
|
1207
1209
|
properties: {},
|
|
1208
|
-
nullable: inputSchema.nullable
|
|
1209
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1210
|
+
nullable: inputSchema.nullable
|
|
1210
1211
|
};
|
|
1212
|
+
if (typeof options.strict === "boolean") {
|
|
1213
|
+
schema.strict = options.strict;
|
|
1214
|
+
}
|
|
1211
1215
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1212
1216
|
if (!schema.properties) {
|
|
1213
1217
|
return;
|
|
@@ -1230,9 +1234,9 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1230
1234
|
return {
|
|
1231
1235
|
...schema,
|
|
1232
1236
|
metadata: {
|
|
1233
|
-
id:
|
|
1234
|
-
description:
|
|
1235
|
-
isDeprecated:
|
|
1237
|
+
id: options.id,
|
|
1238
|
+
description: options.description,
|
|
1239
|
+
isDeprecated: options.isDeprecated,
|
|
1236
1240
|
[SCHEMA_METADATA]: {
|
|
1237
1241
|
output: {},
|
|
1238
1242
|
parse: (input, context) => {
|
|
@@ -1261,14 +1265,16 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1261
1265
|
}
|
|
1262
1266
|
};
|
|
1263
1267
|
}
|
|
1264
|
-
function omit(inputSchema, keys,
|
|
1268
|
+
function omit(inputSchema, keys, options = {}) {
|
|
1265
1269
|
const schema = {
|
|
1266
1270
|
properties: {
|
|
1267
1271
|
...inputSchema.properties
|
|
1268
1272
|
},
|
|
1269
|
-
nullable: inputSchema.nullable
|
|
1270
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1273
|
+
nullable: inputSchema.nullable
|
|
1271
1274
|
};
|
|
1275
|
+
if (typeof options.strict === "boolean") {
|
|
1276
|
+
schema.strict = options.strict;
|
|
1277
|
+
}
|
|
1272
1278
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1273
1279
|
if (keys.includes(key)) {
|
|
1274
1280
|
delete schema.properties[key];
|
|
@@ -1290,9 +1296,9 @@ function omit(inputSchema, keys, opts = {}) {
|
|
|
1290
1296
|
return {
|
|
1291
1297
|
...schema,
|
|
1292
1298
|
metadata: {
|
|
1293
|
-
id:
|
|
1294
|
-
description:
|
|
1295
|
-
isDeprecated:
|
|
1299
|
+
id: options.id,
|
|
1300
|
+
description: options.description,
|
|
1301
|
+
isDeprecated: options.isDeprecated,
|
|
1296
1302
|
[SCHEMA_METADATA]: {
|
|
1297
1303
|
output: {},
|
|
1298
1304
|
validate(input) {
|
|
@@ -1353,7 +1359,7 @@ function serializeObject(schema, input, data) {
|
|
|
1353
1359
|
}
|
|
1354
1360
|
return `{${strParts.join(",")}}`;
|
|
1355
1361
|
}
|
|
1356
|
-
function extend(baseSchema, inputSchema,
|
|
1362
|
+
function extend(baseSchema, inputSchema, options = {}) {
|
|
1357
1363
|
const schema = {
|
|
1358
1364
|
properties: {
|
|
1359
1365
|
...baseSchema.properties,
|
|
@@ -1362,13 +1368,15 @@ function extend(baseSchema, inputSchema, opts = {}) {
|
|
|
1362
1368
|
optionalProperties: {
|
|
1363
1369
|
...baseSchema.optionalProperties,
|
|
1364
1370
|
...inputSchema.optionalProperties
|
|
1365
|
-
}
|
|
1366
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1371
|
+
}
|
|
1367
1372
|
};
|
|
1373
|
+
if (typeof options.strict === "boolean") {
|
|
1374
|
+
schema.strict = options.strict;
|
|
1375
|
+
}
|
|
1368
1376
|
const isType = (input) => validateObjectSchema(schema, input);
|
|
1369
1377
|
const meta = {
|
|
1370
|
-
id:
|
|
1371
|
-
description:
|
|
1378
|
+
id: options.id,
|
|
1379
|
+
description: options.description,
|
|
1372
1380
|
[SCHEMA_METADATA]: {
|
|
1373
1381
|
output: {},
|
|
1374
1382
|
parse(input, context) {
|
|
@@ -1390,9 +1398,11 @@ function partial(schema, options = {}) {
|
|
|
1390
1398
|
const newSchema = {
|
|
1391
1399
|
properties: {},
|
|
1392
1400
|
optionalProperties: {},
|
|
1393
|
-
additionalProperties: typeof options.additionalProperties === "boolean" ? options.additionalProperties : true,
|
|
1394
1401
|
nullable: schema.nullable
|
|
1395
1402
|
};
|
|
1403
|
+
if (typeof options.strict === "boolean") {
|
|
1404
|
+
schema.strict = options.strict;
|
|
1405
|
+
}
|
|
1396
1406
|
for (const key of Object.keys(schema.properties)) {
|
|
1397
1407
|
const prop = schema.properties[key];
|
|
1398
1408
|
newSchema.optionalProperties[key] = optional(prop);
|
|
@@ -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,11 +1041,13 @@ 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
|
-
properties: {}
|
|
1047
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1046
|
+
properties: {}
|
|
1048
1047
|
};
|
|
1048
|
+
if (typeof options.strict === "boolean") {
|
|
1049
|
+
schema.strict = options.strict;
|
|
1050
|
+
}
|
|
1049
1051
|
for (const key of Object.keys(input)) {
|
|
1050
1052
|
const prop = input[key];
|
|
1051
1053
|
if (prop.metadata[SCHEMA_METADATA].optional) {
|
|
@@ -1060,9 +1062,9 @@ function object(propA, propB) {
|
|
|
1060
1062
|
const result = {
|
|
1061
1063
|
...schema,
|
|
1062
1064
|
metadata: {
|
|
1063
|
-
id:
|
|
1064
|
-
description:
|
|
1065
|
-
isDeprecated:
|
|
1065
|
+
id: options.id,
|
|
1066
|
+
description: options.description,
|
|
1067
|
+
isDeprecated: options.isDeprecated,
|
|
1066
1068
|
[SCHEMA_METADATA]: {
|
|
1067
1069
|
output: {},
|
|
1068
1070
|
parse(input2, context) {
|
|
@@ -1073,11 +1075,11 @@ function object(propA, propB) {
|
|
|
1073
1075
|
false
|
|
1074
1076
|
);
|
|
1075
1077
|
},
|
|
1076
|
-
coerce(input2,
|
|
1078
|
+
coerce(input2, context) {
|
|
1077
1079
|
return parseObjectSchema(
|
|
1078
1080
|
schema,
|
|
1079
1081
|
input2,
|
|
1080
|
-
|
|
1082
|
+
context,
|
|
1081
1083
|
true
|
|
1082
1084
|
);
|
|
1083
1085
|
},
|
|
@@ -1102,7 +1104,7 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1102
1104
|
try {
|
|
1103
1105
|
const result2 = JSON.parse(input);
|
|
1104
1106
|
parsedInput = result2;
|
|
1105
|
-
} catch (
|
|
1107
|
+
} catch (_err) {
|
|
1106
1108
|
data.errors.push({
|
|
1107
1109
|
instancePath: data.instancePath,
|
|
1108
1110
|
schemaPath: `${data.schemaPath}/properties`,
|
|
@@ -1124,13 +1126,13 @@ function parseObjectSchema(schema, input, data, coerce = false) {
|
|
|
1124
1126
|
const inputKeys = Object.keys(parsedInput);
|
|
1125
1127
|
const requiredKeys = Object.keys(schema.properties);
|
|
1126
1128
|
const optionalKeys = Object.keys(optionalProps);
|
|
1127
|
-
if (
|
|
1129
|
+
if (schema.strict) {
|
|
1128
1130
|
for (const key of inputKeys) {
|
|
1129
1131
|
if (!requiredKeys.includes(key) && !optionalKeys.includes(key) && key !== data.discriminatorKey) {
|
|
1130
1132
|
data.errors.push({
|
|
1131
1133
|
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
|
|
1134
|
+
schemaPath: `${data.schemaPath}/strict`,
|
|
1135
|
+
message: `Error at ${data.instancePath}/${key}. Key '${key}' is not included in the schema. To allow additional input properties set strict to 'false'.`
|
|
1134
1136
|
});
|
|
1135
1137
|
}
|
|
1136
1138
|
}
|
|
@@ -1192,7 +1194,7 @@ function validateObjectSchema(schema, input) {
|
|
|
1192
1194
|
const optionalProps = schema.optionalProperties ?? {};
|
|
1193
1195
|
for (const key of Object.keys(input)) {
|
|
1194
1196
|
const prop = schema.properties[key] ?? optionalProps[key];
|
|
1195
|
-
if (!prop &&
|
|
1197
|
+
if (!prop && schema.strict) {
|
|
1196
1198
|
return false;
|
|
1197
1199
|
}
|
|
1198
1200
|
if (prop) {
|
|
@@ -1204,12 +1206,14 @@ function validateObjectSchema(schema, input) {
|
|
|
1204
1206
|
}
|
|
1205
1207
|
return true;
|
|
1206
1208
|
}
|
|
1207
|
-
function pick(inputSchema, keys,
|
|
1209
|
+
function pick(inputSchema, keys, options = {}) {
|
|
1208
1210
|
const schema = {
|
|
1209
1211
|
properties: {},
|
|
1210
|
-
nullable: inputSchema.nullable
|
|
1211
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1212
|
+
nullable: inputSchema.nullable
|
|
1212
1213
|
};
|
|
1214
|
+
if (typeof options.strict === "boolean") {
|
|
1215
|
+
schema.strict = options.strict;
|
|
1216
|
+
}
|
|
1213
1217
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1214
1218
|
if (!schema.properties) {
|
|
1215
1219
|
return;
|
|
@@ -1232,9 +1236,9 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1232
1236
|
return {
|
|
1233
1237
|
...schema,
|
|
1234
1238
|
metadata: {
|
|
1235
|
-
id:
|
|
1236
|
-
description:
|
|
1237
|
-
isDeprecated:
|
|
1239
|
+
id: options.id,
|
|
1240
|
+
description: options.description,
|
|
1241
|
+
isDeprecated: options.isDeprecated,
|
|
1238
1242
|
[SCHEMA_METADATA]: {
|
|
1239
1243
|
output: {},
|
|
1240
1244
|
parse: (input, context) => {
|
|
@@ -1263,14 +1267,16 @@ function pick(inputSchema, keys, opts = {}) {
|
|
|
1263
1267
|
}
|
|
1264
1268
|
};
|
|
1265
1269
|
}
|
|
1266
|
-
function omit(inputSchema, keys,
|
|
1270
|
+
function omit(inputSchema, keys, options = {}) {
|
|
1267
1271
|
const schema = {
|
|
1268
1272
|
properties: {
|
|
1269
1273
|
...inputSchema.properties
|
|
1270
1274
|
},
|
|
1271
|
-
nullable: inputSchema.nullable
|
|
1272
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1275
|
+
nullable: inputSchema.nullable
|
|
1273
1276
|
};
|
|
1277
|
+
if (typeof options.strict === "boolean") {
|
|
1278
|
+
schema.strict = options.strict;
|
|
1279
|
+
}
|
|
1274
1280
|
Object.keys(inputSchema.properties).forEach((key) => {
|
|
1275
1281
|
if (keys.includes(key)) {
|
|
1276
1282
|
delete schema.properties[key];
|
|
@@ -1292,9 +1298,9 @@ function omit(inputSchema, keys, opts = {}) {
|
|
|
1292
1298
|
return {
|
|
1293
1299
|
...schema,
|
|
1294
1300
|
metadata: {
|
|
1295
|
-
id:
|
|
1296
|
-
description:
|
|
1297
|
-
isDeprecated:
|
|
1301
|
+
id: options.id,
|
|
1302
|
+
description: options.description,
|
|
1303
|
+
isDeprecated: options.isDeprecated,
|
|
1298
1304
|
[SCHEMA_METADATA]: {
|
|
1299
1305
|
output: {},
|
|
1300
1306
|
validate(input) {
|
|
@@ -1355,7 +1361,7 @@ function serializeObject(schema, input, data) {
|
|
|
1355
1361
|
}
|
|
1356
1362
|
return `{${strParts.join(",")}}`;
|
|
1357
1363
|
}
|
|
1358
|
-
function extend(baseSchema, inputSchema,
|
|
1364
|
+
function extend(baseSchema, inputSchema, options = {}) {
|
|
1359
1365
|
const schema = {
|
|
1360
1366
|
properties: {
|
|
1361
1367
|
...baseSchema.properties,
|
|
@@ -1364,13 +1370,15 @@ function extend(baseSchema, inputSchema, opts = {}) {
|
|
|
1364
1370
|
optionalProperties: {
|
|
1365
1371
|
...baseSchema.optionalProperties,
|
|
1366
1372
|
...inputSchema.optionalProperties
|
|
1367
|
-
}
|
|
1368
|
-
additionalProperties: typeof opts.additionalProperties === "boolean" ? opts.additionalProperties : true
|
|
1373
|
+
}
|
|
1369
1374
|
};
|
|
1375
|
+
if (typeof options.strict === "boolean") {
|
|
1376
|
+
schema.strict = options.strict;
|
|
1377
|
+
}
|
|
1370
1378
|
const isType = (input) => validateObjectSchema(schema, input);
|
|
1371
1379
|
const meta = {
|
|
1372
|
-
id:
|
|
1373
|
-
description:
|
|
1380
|
+
id: options.id,
|
|
1381
|
+
description: options.description,
|
|
1374
1382
|
[SCHEMA_METADATA]: {
|
|
1375
1383
|
output: {},
|
|
1376
1384
|
parse(input, context) {
|
|
@@ -1392,9 +1400,11 @@ function partial(schema, options = {}) {
|
|
|
1392
1400
|
const newSchema = {
|
|
1393
1401
|
properties: {},
|
|
1394
1402
|
optionalProperties: {},
|
|
1395
|
-
additionalProperties: typeof options.additionalProperties === "boolean" ? options.additionalProperties : true,
|
|
1396
1403
|
nullable: schema.nullable
|
|
1397
1404
|
};
|
|
1405
|
+
if (typeof options.strict === "boolean") {
|
|
1406
|
+
schema.strict = options.strict;
|
|
1407
|
+
}
|
|
1398
1408
|
for (const key of Object.keys(schema.properties)) {
|
|
1399
1409
|
const prop = schema.properties[key];
|
|
1400
1410
|
newSchema.optionalProperties[key] = optional(prop);
|
|
@@ -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.78a649a7.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.31273dd5.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.1",
|
|
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.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@faker-js/faker": "^8.4.1",
|