@arrirpc/schema 0.75.2 → 0.76.2-beta.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 CHANGED
@@ -1010,9 +1010,12 @@ $$User.serializeUnsafe({ id: '1', email: null, created: new Date() });
1010
1010
 
1011
1011
  In most cases, the compiled validators will be much faster than the standard utilities. However there is some overhead with compiling the schemas so ideally each validator would be compiled once. Additionally the resulting methods are created using [`new Function()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) so they can only be used in an environment that you control such as a backend server. They WILL NOT work in a browser environment.
1012
1012
 
1013
- You can also use `a.compile` for code generation. The compiler result gives you access to the generated function bodies.
1013
+ You can also use `a.compile` for code generation. Passing `true` as the second parameter will ensure that the compile result gives you access to the generated function bodies. This is disabled by default as of `v0.76.0` in order to reduce unwanted memory usage. (No need to carry around these large strings if they aren't going to be used.)
1014
1014
 
1015
1015
  ```ts
1016
+ // pass true to the second parameter to get access to the generated function bodies
1017
+ const $$User = a.compile(User, true);
1018
+
1016
1019
  $$User.compiledCode.validate; // the generated validation code
1017
1020
  $$User.compiledCode.parse; // the generated parsing code
1018
1021
  $$User.compiledCode.coerce; // the generated coercion code
package/dist/index.cjs CHANGED
@@ -1460,7 +1460,7 @@ function isValidationException(input) {
1460
1460
  return input instanceof ValidationException;
1461
1461
  }
1462
1462
 
1463
- function compile(schema) {
1463
+ function compile(schema, includeCompiledCode) {
1464
1464
  const validateCode = createValidationTemplate("input", schema);
1465
1465
  const parser = getCompiledParser("input", schema, false);
1466
1466
  const parserFn = parser.fn;
@@ -1577,12 +1577,12 @@ function compile(schema) {
1577
1577
  serializeUnsafe(input) {
1578
1578
  return serializeFn(input);
1579
1579
  },
1580
- compiledCode: {
1580
+ compiledCode: includeCompiledCode === true ? {
1581
1581
  validate: validateCode,
1582
1582
  parse: parser.code,
1583
1583
  coerce: coercer.code,
1584
1584
  serialize: serializer.code
1585
- },
1585
+ } : void 0,
1586
1586
  "~standard": adapters.createStandardSchemaProperty(validate, (input, ctx) => {
1587
1587
  const result2 = parse(input);
1588
1588
  if (!result2.success) {
package/dist/index.d.cts CHANGED
@@ -9,7 +9,13 @@ declare function createSerializationV2Template(inputName: string, schema: Schema
9
9
 
10
10
  declare function createValidationTemplate(inputName: string, schema: ASchema<any>): string;
11
11
 
12
- interface CompiledValidator<TSchema extends ASchema<any>> {
12
+ interface CompiledValidatorCompiledCode {
13
+ parse: string;
14
+ coerce: string;
15
+ serialize: string;
16
+ validate: string;
17
+ }
18
+ interface CompiledValidator<TSchema extends ASchema<any>, TIncludeCode extends boolean = false> {
13
19
  schema: ASchemaStrict<TSchema>;
14
20
  /**
15
21
  * Determine if a type matches a schema. This is a type guard.
@@ -42,18 +48,13 @@ interface CompiledValidator<TSchema extends ASchema<any>> {
42
48
  /**
43
49
  * The ATD Schema
44
50
  */
45
- compiledCode: {
46
- parse: string;
47
- coerce: string;
48
- serialize: string;
49
- validate: string;
50
- };
51
+ compiledCode: TIncludeCode extends true ? CompiledValidatorCompiledCode : undefined;
51
52
  }
52
- type CompiledValidatorWithAdapters<TSchema extends ASchema> = CompiledValidator<TSchema> & StandardSchemaV1<InferType<TSchema>>;
53
+ type CompiledValidatorWithAdapters<TSchema extends ASchema, TIncludeCode extends boolean = false> = CompiledValidator<TSchema, TIncludeCode> & StandardSchemaV1<InferType<TSchema>>;
53
54
  /**
54
55
  * Create compiled versions of the `decode()`, `validate()`, and `serialize()` functions
55
56
  */
56
- declare function compile<TSchema extends ASchema<any>>(schema: TSchema): CompiledValidatorWithAdapters<TSchema>;
57
+ declare function compile<TSchema extends ASchema<any>, TIncludeCompiled extends boolean = false>(schema: TSchema, includeCompiledCode?: TIncludeCompiled): CompiledValidatorWithAdapters<TSchema, TIncludeCompiled>;
57
58
  type CompiledParser<TSchema extends ASchema<any>> = SchemaValidator<InferType<TSchema>>['parse'];
58
59
  declare function getCompiledParser<TSchema extends ASchema<any>>(input: string, schema: Schema, shouldCoerce: boolean): {
59
60
  fn: CompiledParser<TSchema>;
@@ -405,4 +406,4 @@ declare const int64Max: bigint;
405
406
  declare const uint64Min: bigint;
406
407
  declare const uint64Max: bigint;
407
408
 
408
- export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
409
+ export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, type CompiledValidatorCompiledCode, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
package/dist/index.d.mts CHANGED
@@ -9,7 +9,13 @@ declare function createSerializationV2Template(inputName: string, schema: Schema
9
9
 
10
10
  declare function createValidationTemplate(inputName: string, schema: ASchema<any>): string;
11
11
 
12
- interface CompiledValidator<TSchema extends ASchema<any>> {
12
+ interface CompiledValidatorCompiledCode {
13
+ parse: string;
14
+ coerce: string;
15
+ serialize: string;
16
+ validate: string;
17
+ }
18
+ interface CompiledValidator<TSchema extends ASchema<any>, TIncludeCode extends boolean = false> {
13
19
  schema: ASchemaStrict<TSchema>;
14
20
  /**
15
21
  * Determine if a type matches a schema. This is a type guard.
@@ -42,18 +48,13 @@ interface CompiledValidator<TSchema extends ASchema<any>> {
42
48
  /**
43
49
  * The ATD Schema
44
50
  */
45
- compiledCode: {
46
- parse: string;
47
- coerce: string;
48
- serialize: string;
49
- validate: string;
50
- };
51
+ compiledCode: TIncludeCode extends true ? CompiledValidatorCompiledCode : undefined;
51
52
  }
52
- type CompiledValidatorWithAdapters<TSchema extends ASchema> = CompiledValidator<TSchema> & StandardSchemaV1<InferType<TSchema>>;
53
+ type CompiledValidatorWithAdapters<TSchema extends ASchema, TIncludeCode extends boolean = false> = CompiledValidator<TSchema, TIncludeCode> & StandardSchemaV1<InferType<TSchema>>;
53
54
  /**
54
55
  * Create compiled versions of the `decode()`, `validate()`, and `serialize()` functions
55
56
  */
56
- declare function compile<TSchema extends ASchema<any>>(schema: TSchema): CompiledValidatorWithAdapters<TSchema>;
57
+ declare function compile<TSchema extends ASchema<any>, TIncludeCompiled extends boolean = false>(schema: TSchema, includeCompiledCode?: TIncludeCompiled): CompiledValidatorWithAdapters<TSchema, TIncludeCompiled>;
57
58
  type CompiledParser<TSchema extends ASchema<any>> = SchemaValidator<InferType<TSchema>>['parse'];
58
59
  declare function getCompiledParser<TSchema extends ASchema<any>>(input: string, schema: Schema, shouldCoerce: boolean): {
59
60
  fn: CompiledParser<TSchema>;
@@ -405,4 +406,4 @@ declare const int64Max: bigint;
405
406
  declare const uint64Min: bigint;
406
407
  declare const uint64Max: bigint;
407
408
 
408
- export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
409
+ export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, type CompiledValidatorCompiledCode, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
package/dist/index.d.ts CHANGED
@@ -9,7 +9,13 @@ declare function createSerializationV2Template(inputName: string, schema: Schema
9
9
 
10
10
  declare function createValidationTemplate(inputName: string, schema: ASchema<any>): string;
11
11
 
12
- interface CompiledValidator<TSchema extends ASchema<any>> {
12
+ interface CompiledValidatorCompiledCode {
13
+ parse: string;
14
+ coerce: string;
15
+ serialize: string;
16
+ validate: string;
17
+ }
18
+ interface CompiledValidator<TSchema extends ASchema<any>, TIncludeCode extends boolean = false> {
13
19
  schema: ASchemaStrict<TSchema>;
14
20
  /**
15
21
  * Determine if a type matches a schema. This is a type guard.
@@ -42,18 +48,13 @@ interface CompiledValidator<TSchema extends ASchema<any>> {
42
48
  /**
43
49
  * The ATD Schema
44
50
  */
45
- compiledCode: {
46
- parse: string;
47
- coerce: string;
48
- serialize: string;
49
- validate: string;
50
- };
51
+ compiledCode: TIncludeCode extends true ? CompiledValidatorCompiledCode : undefined;
51
52
  }
52
- type CompiledValidatorWithAdapters<TSchema extends ASchema> = CompiledValidator<TSchema> & StandardSchemaV1<InferType<TSchema>>;
53
+ type CompiledValidatorWithAdapters<TSchema extends ASchema, TIncludeCode extends boolean = false> = CompiledValidator<TSchema, TIncludeCode> & StandardSchemaV1<InferType<TSchema>>;
53
54
  /**
54
55
  * Create compiled versions of the `decode()`, `validate()`, and `serialize()` functions
55
56
  */
56
- declare function compile<TSchema extends ASchema<any>>(schema: TSchema): CompiledValidatorWithAdapters<TSchema>;
57
+ declare function compile<TSchema extends ASchema<any>, TIncludeCompiled extends boolean = false>(schema: TSchema, includeCompiledCode?: TIncludeCompiled): CompiledValidatorWithAdapters<TSchema, TIncludeCompiled>;
57
58
  type CompiledParser<TSchema extends ASchema<any>> = SchemaValidator<InferType<TSchema>>['parse'];
58
59
  declare function getCompiledParser<TSchema extends ASchema<any>>(input: string, schema: Schema, shouldCoerce: boolean): {
59
60
  fn: CompiledParser<TSchema>;
@@ -405,4 +406,4 @@ declare const int64Max: bigint;
405
406
  declare const uint64Min: bigint;
406
407
  declare const uint64Max: bigint;
407
408
 
408
- export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
409
+ export { AArraySchema, ADiscriminatorSchemaWithAdapters, AObjectSchema, AObjectSchemaOptions, AObjectSchemaWithAdapters, ARecordSchemaWithAdapters, ARefSchema, AScalarSchemaWithAdapters, ASchema, ASchemaOptions, ASchemaStrict, ASchemaWithAdapters, AStringEnumSchemaWithAdapters, type CompiledValidator, type CompiledValidatorCompiledCode, InferObjectOutput, InferSubType, InferType, ResolveObject, Result, STR_ESCAPE, SchemaValidator, ValidationContext, ValueError, WithAdapters, _namespace as a, any, array, boolean, clone, coerce, coerceUnsafe, compile, decodeObjectSchema, discriminator, enumerator, errors, extend, float32, float64, getCompiledParser, getCompiledSerializer, createParsingTemplate as getSchemaDecodingCode, createSerializationV2Template as getSchemaSerializationCode, createValidationTemplate as getSchemaValidationCode, int16, int16Max, int16Min, int32, int32Max, int32Min, int64, int64Max, int64Min, int8, int8Max, int8Min, nullable, number, object, omit, optional, parse, parseUnsafe, partial, pick, record, recursive, sanitizeJson, serialize, serializeObject, serializeSmallString, serializeString, serializeUnsafe, string, stringEnum, timestamp, uint16, uint16Max, uint16Min, uint32, uint32Max, uint32Min, uint64, uint64Max, uint64Min, uint8, uint8Max, uint8Min, validate, validateObjectSchema };
package/dist/index.mjs CHANGED
@@ -1459,7 +1459,7 @@ function isValidationException(input) {
1459
1459
  return input instanceof ValidationException;
1460
1460
  }
1461
1461
 
1462
- function compile(schema) {
1462
+ function compile(schema, includeCompiledCode) {
1463
1463
  const validateCode = createValidationTemplate("input", schema);
1464
1464
  const parser = getCompiledParser("input", schema, false);
1465
1465
  const parserFn = parser.fn;
@@ -1576,12 +1576,12 @@ function compile(schema) {
1576
1576
  serializeUnsafe(input) {
1577
1577
  return serializeFn(input);
1578
1578
  },
1579
- compiledCode: {
1579
+ compiledCode: includeCompiledCode === true ? {
1580
1580
  validate: validateCode,
1581
1581
  parse: parser.code,
1582
1582
  coerce: coercer.code,
1583
1583
  serialize: serializer.code
1584
- },
1584
+ } : void 0,
1585
1585
  "~standard": createStandardSchemaProperty(validate, (input, ctx) => {
1586
1586
  const result2 = parse(input);
1587
1587
  if (!result2.success) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/schema",
3
- "version": "0.75.2",
3
+ "version": "0.76.2-beta.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -25,7 +25,7 @@
25
25
  "@standard-schema/spec": "1.0.0",
26
26
  "scule": "^1.3.0",
27
27
  "uncrypto": "^0.1.3",
28
- "@arrirpc/type-defs": "0.75.2"
28
+ "@arrirpc/type-defs": "0.76.2-beta.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "ajv": "^8.17.1",