@arrirpc/schema 0.73.0 → 0.74.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -22
- package/dist/adapters.cjs +9 -0
- package/dist/adapters.d.cts +11 -0
- package/dist/adapters.d.mts +11 -0
- package/dist/adapters.d.ts +11 -0
- package/dist/adapters.mjs +2 -0
- package/dist/index.cjs +2610 -500
- package/dist/index.d.cts +100 -63
- package/dist/index.d.mts +100 -63
- package/dist/index.d.ts +100 -63
- package/dist/index.mjs +2512 -402
- package/dist/shared/{schema.DLX0nTwB.d.cts → schema.BYXPajpl.d.cts} +54 -51
- package/dist/shared/{schema.DLX0nTwB.d.mts → schema.BYXPajpl.d.mts} +54 -51
- package/dist/shared/{schema.DLX0nTwB.d.ts → schema.BYXPajpl.d.ts} +54 -51
- package/dist/shared/schema.CZrExOkx.mjs +96 -0
- package/dist/shared/schema.Cwqn0t9l.cjs +111 -0
- package/package.json +3 -3
- package/dist/shared/schema.COiEWm_0.cjs +0 -1935
- package/dist/shared/schema.PNAcAdz3.mjs +0 -1860
- package/dist/testSuites.cjs +0 -1310
- package/dist/testSuites.d.cts +0 -39
- package/dist/testSuites.d.mts +0 -39
- package/dist/testSuites.d.ts +0 -39
- package/dist/testSuites.mjs +0 -1305
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
|
-
- [
|
|
49
|
+
- [Parse Unsafe](#parse-unsafe)
|
|
50
50
|
- [Coerce](#coerce)
|
|
51
|
-
- [
|
|
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)
|
|
@@ -748,13 +749,20 @@ const User = a.object({
|
|
|
748
749
|
name: a.string(),
|
|
749
750
|
});
|
|
750
751
|
|
|
751
|
-
// returns
|
|
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
|
-
###
|
|
763
|
+
### Parse Unsafe
|
|
756
764
|
|
|
757
|
-
|
|
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
|
-
|
|
766
|
-
|
|
767
|
-
|
|
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.
|
|
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:
|
|
794
|
+
// { success: true, value: { a: '1', b: true, c: 500.24 } };
|
|
790
795
|
```
|
|
791
796
|
|
|
792
|
-
###
|
|
797
|
+
### Coerce Unsafe
|
|
793
798
|
|
|
794
|
-
`a.
|
|
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
|
-
|
|
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
|
-
|
|
808
|
-
console.error(result.error);
|
|
824
|
+
// '{"id":"1","name":"john doe"}''
|
|
809
825
|
}
|
|
810
826
|
```
|
|
811
827
|
|
|
812
|
-
|
|
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.
|
|
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,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 };
|