@arrirpc/schema 0.72.0 → 0.74.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -46,10 +46,11 @@ Originally this library was created as a way for building schemas for [Json Type
46
46
  - [Utilities](#utilities)
47
47
  - [Validate](#validate)
48
48
  - [Parse](#parse)
49
- - [Safe Parse](#safe-parse)
49
+ - [Parse Unsafe](#parse-unsafe)
50
50
  - [Coerce](#coerce)
51
- - [Safe Coerce](#safe-coerce)
51
+ - [Coerce Unsafe](#coerce-unsafe)
52
52
  - [Serialize](#serialize)
53
+ - [Serialize Unsafe](#serialize-unsafe)
53
54
  - [Errors](#errors)
54
55
  - [Metadata](#metadata)
55
56
  - [Compiled Validators](#compiled-validators)
@@ -328,12 +329,12 @@ const User = a.object({
328
329
  });
329
330
  type User = a.infer<typeof User>; // { id: string; email: string; created: Date; }
330
331
 
331
- a.validate({
332
+ a.validate(User, {
332
333
  id: '1',
333
334
  email: 'johndoe@example.com',
334
335
  created: new Date(),
335
336
  }); // true
336
- a.validate({
337
+ a.validate(User, {
337
338
  id: '1',
338
339
  email: null,
339
340
  created: new Date(),
@@ -748,13 +749,20 @@ const User = a.object({
748
749
  name: a.string(),
749
750
  });
750
751
 
751
- // returns a User if successful or throws a ValidationError if fails
752
+ // returns Result<User>
752
753
  const result = a.parse(User, jsonString);
754
+ if (result.success) {
755
+ // something when wrong with parsing
756
+ console.log(result.errors);
757
+ } else {
758
+ // parsing was successful
759
+ console.log(result.value);
760
+ }
753
761
  ```
754
762
 
755
- ### Safe Parse
763
+ ### Parse Unsafe
756
764
 
757
- A safer alternative to `a.parse()` that doesn't throw an error.
765
+ Alternate version to `parse()` that will throw a `ValidationException` if parsing fails.
758
766
 
759
767
  ```ts
760
768
  const User = a.object({
@@ -762,17 +770,14 @@ const User = a.object({
762
770
  name: a.string(),
763
771
  });
764
772
 
765
- const result = a.safeParse(User, jsonString);
766
- if (result.success) {
767
- console.log(result.value); // result.value will be User
768
- } else {
769
- console.error(result.error);
770
- }
773
+ // can throw an error
774
+ const result = a.parseUnsafe(User, jsonString);
775
+ console.log(result);
771
776
  ```
772
777
 
773
778
  ### Coerce
774
779
 
775
- `a.coerce()` will attempt to convert inputs to the correct type. If it fails to convert the inputs it will throw a `ValidationError`
780
+ `a.coerce()` will attempt to convert inputs to the correct type. Returns a `Result<T>`
776
781
 
777
782
  ```ts
778
783
  const A = a.object({
@@ -786,12 +791,12 @@ a.coerce(A, {
786
791
  b: 'true',
787
792
  c: '500.24',
788
793
  });
789
- // { a: "1", b: true, c: 500.24 };
794
+ // { success: true, value: { a: '1', b: true, c: 500.24 } };
790
795
  ```
791
796
 
792
- ### Safe Coerce
797
+ ### Coerce Unsafe
793
798
 
794
- `a.safeCoerce()` is an alternative to `a.coerce()` that doesn't throw.
799
+ `a.coerceUnsafe()` is an alternative to `a.coerce()` that will throw an error if coercion fails
795
800
 
796
801
  ```ts
797
802
  const A = a.object({
@@ -800,18 +805,31 @@ const A = a.object({
800
805
  c: a.float32(),
801
806
  });
802
807
 
803
- const result = a.safeCoerce(A, someInput);
808
+ a.coerceUnsafe(A, someInput); // returns T but can throw an error
809
+ ```
810
+
811
+ ### Serialize
812
+
813
+ `a.serialize()` will take an input and serialize it to a valid JSON string. This returns `Result<string>`
804
814
 
815
+ ```ts
816
+ const User = a.object({
817
+ id: a.string(),
818
+ name: a.string(),
819
+ });
820
+
821
+ const result = a.serialize(User, { id: '1', name: 'john doe' });
805
822
  if (result.success) {
806
823
  console.log(result.value);
807
- } else {
808
- console.error(result.error);
824
+ // '{"id":"1","name":"john doe"}''
809
825
  }
810
826
  ```
811
827
 
812
- ### Serialize
828
+ Be aware that this function does not validate the input. So if you are passing in an any or unknown type into this function it is recommended that you validate it first.
829
+
830
+ ### Serialize Unsafe
813
831
 
814
- `a.serialize()` will take an input and serialize it to a valid JSON string.
832
+ `a.serializeUnsafe()` is an alternative to `a.serialize()` that returns a JSON string, but can throw an error.
815
833
 
816
834
  ```ts
817
835
  const User = a.object({
@@ -819,8 +837,8 @@ const User = a.object({
819
837
  name: a.string(),
820
838
  });
821
839
 
822
- a.serialize(User, { id: '1', name: 'john doe' });
823
- // {"id":"1","name":"john doe"}
840
+ const result = a.serialize(User, { id: '1', name: 'john doe' }); // might throw an error
841
+ // '{"id":"1","name":"john doe"}''
824
842
  ```
825
843
 
826
844
  Be aware that this function does not validate the input. So if you are passing in an any or unknown type into this function it is recommended that you validate it first.
@@ -983,7 +1001,9 @@ const $$User = a.compile(User);
983
1001
 
984
1002
  $$User.validate(someInput);
985
1003
  $$User.parse(someJson);
1004
+ $$User.parseUnsafe(someJson);
986
1005
  $$User.serialize({ id: '1', email: null, created: new Date() });
1006
+ $$User.serializeUnsafe({ id: '1', email: null, created: new Date() });
987
1007
  ```
988
1008
 
989
1009
  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.
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const adapters = require('./shared/schema.Cwqn0t9l.cjs');
4
+ require('@arrirpc/type-defs');
5
+
6
+
7
+
8
+ exports.createStandardSchemaProperty = adapters.createStandardSchemaProperty;
9
+ exports.hideInvalidProperties = adapters.hideInvalidProperties;
@@ -0,0 +1,11 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { V as ValidationContext, A as ASchema } from './shared/schema.BYXPajpl.cjs';
3
+ import '@arrirpc/type-defs';
4
+
5
+ declare function createStandardSchemaProperty<T>(validate: (input: unknown) => input is T, parse: (input: unknown, ctx: ValidationContext) => T | undefined, vendor?: string): StandardSchemaV1<T>['~standard'];
6
+ /**
7
+ * Ensure that non ATD compliant properties get hidden when serialized to JSON
8
+ */
9
+ declare function hideInvalidProperties(schema: ASchema): void;
10
+
11
+ export { createStandardSchemaProperty, hideInvalidProperties };
@@ -0,0 +1,11 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { V as ValidationContext, A as ASchema } from './shared/schema.BYXPajpl.mjs';
3
+ import '@arrirpc/type-defs';
4
+
5
+ declare function createStandardSchemaProperty<T>(validate: (input: unknown) => input is T, parse: (input: unknown, ctx: ValidationContext) => T | undefined, vendor?: string): StandardSchemaV1<T>['~standard'];
6
+ /**
7
+ * Ensure that non ATD compliant properties get hidden when serialized to JSON
8
+ */
9
+ declare function hideInvalidProperties(schema: ASchema): void;
10
+
11
+ export { createStandardSchemaProperty, hideInvalidProperties };
@@ -0,0 +1,11 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { V as ValidationContext, A as ASchema } from './shared/schema.BYXPajpl.js';
3
+ import '@arrirpc/type-defs';
4
+
5
+ declare function createStandardSchemaProperty<T>(validate: (input: unknown) => input is T, parse: (input: unknown, ctx: ValidationContext) => T | undefined, vendor?: string): StandardSchemaV1<T>['~standard'];
6
+ /**
7
+ * Ensure that non ATD compliant properties get hidden when serialized to JSON
8
+ */
9
+ declare function hideInvalidProperties(schema: ASchema): void;
10
+
11
+ export { createStandardSchemaProperty, hideInvalidProperties };
@@ -0,0 +1,2 @@
1
+ export { g as createStandardSchemaProperty, h as hideInvalidProperties } from './shared/schema.CZrExOkx.mjs';
2
+ import '@arrirpc/type-defs';