@arrirpc/typebox-adapter 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/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const schema = require('@arrirpc/schema');
4
+ const adapters = require('@arrirpc/schema/dist/adapters');
4
5
  const typebox = require('@sinclair/typebox');
5
6
  const compiler = require('@sinclair/typebox/compiler');
6
7
  const value = require('@sinclair/typebox/value');
@@ -9,70 +10,64 @@ const jsonSchemaToAtd = require('json-schema-to-atd');
9
10
  function typeboxAdapter(input) {
10
11
  const schema$1 = jsonSchemaToAtd.jsonSchemaToJtdSchema(input);
11
12
  const compiled = compiler.TypeCompiler.Compile(input);
12
- const result = {
13
- ...schema$1,
14
- metadata: {
15
- id: input.$id ?? input.title,
16
- description: input.description,
17
- [schema.SCHEMA_METADATA]: {
18
- _isAdapted: true,
19
- output: {},
20
- optional: input[typebox.OptionalKind] === "Optional",
21
- parse(val) {
22
- if (typeof val === "string") {
23
- const parsedVal = JSON.parse(val);
24
- if (compiled.Check(parsedVal)) {
25
- return parsedVal;
26
- }
27
- throw typeboxErrorsToArriError(
28
- compiled.Errors(parsedVal)
29
- );
30
- }
31
- if (compiled.Check(val)) {
32
- return val;
33
- }
34
- throw typeboxErrorsToArriError(compiled.Errors(val));
35
- },
36
- coerce(val) {
37
- return value.Value.Cast(input, val);
38
- },
39
- validate(val) {
40
- return compiled.Check(val);
41
- },
42
- serialize(val) {
43
- return compiled.Encode(val);
13
+ const validationMethods = {
14
+ output: {},
15
+ optional: input[typebox.OptionalKind] === "Optional",
16
+ parse(val, context) {
17
+ if (typeof val === "string") {
18
+ const parsedVal = JSON.parse(val);
19
+ if (compiled.Check(parsedVal)) {
20
+ return parsedVal;
44
21
  }
22
+ context.errors = typeboxErrorsToArriErrors(
23
+ compiled.Errors(parsedVal)
24
+ );
25
+ return undefined;
45
26
  }
27
+ if (compiled.Check(val)) {
28
+ return val;
29
+ }
30
+ context.errors = typeboxErrorsToArriErrors(compiled.Errors(val));
31
+ return undefined;
46
32
  },
47
- "~standard": {
48
- version: 1,
49
- vendor: "arri-typebox",
50
- validate(value) {
51
- if (compiled.Check(value)) {
52
- return {
53
- value
54
- };
55
- }
56
- if (typeof value === "string") {
57
- const parsedVal = JSON.parse(value);
58
- if (compiled.Check(parsedVal)) {
59
- return parsedVal;
60
- }
61
- }
62
- const errors = typeboxErrorsToArriError(compiled.Errors(value));
63
- return {
64
- issues: errors.errors.map((err) => ({
65
- message: err.message ?? "Unknown error",
66
- path: err.instancePath.split("/").filter((item) => item.length > 0)
67
- }))
68
- };
33
+ coerce(val, context) {
34
+ try {
35
+ return value.Value.Cast(input, val);
36
+ } catch (err) {
37
+ context.errors.push({
38
+ instancePath: "/",
39
+ message: err instanceof Error ? err.message : `${err}`
40
+ });
41
+ return undefined;
42
+ }
43
+ },
44
+ validate(val) {
45
+ return compiled.Check(val);
46
+ },
47
+ serialize(val, context) {
48
+ try {
49
+ return compiled.Encode(val);
50
+ } catch (err) {
51
+ context.errors.push({
52
+ instancePath: "/",
53
+ message: err instanceof Error ? err.message : `${err}`
54
+ });
55
+ return undefined;
69
56
  }
70
57
  }
71
58
  };
72
- schema.hideInvalidProperties(result);
59
+ const result = {
60
+ ...schema$1,
61
+ metadata: {
62
+ id: input.$id ?? input.title,
63
+ description: input.description
64
+ },
65
+ [schema.VALIDATOR_KEY]: validationMethods
66
+ };
67
+ adapters.hideInvalidProperties(result);
73
68
  return result;
74
69
  }
75
- function typeboxErrorsToArriError(errs) {
70
+ function typeboxErrorsToArriErrors(errs) {
76
71
  const mappedErrs = [];
77
72
  for (const err of errs) {
78
73
  const obj = {
@@ -83,10 +78,7 @@ function typeboxErrorsToArriError(errs) {
83
78
  };
84
79
  mappedErrs.push(obj);
85
80
  }
86
- return new schema.ValidationError({
87
- message: "Error validating input",
88
- errors: mappedErrs
89
- });
81
+ return mappedErrs;
90
82
  }
91
83
 
92
84
  exports.typeboxAdapter = typeboxAdapter;
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { SCHEMA_METADATA, hideInvalidProperties, ValidationError } from '@arrirpc/schema';
1
+ import { VALIDATOR_KEY } from '@arrirpc/schema';
2
+ import { hideInvalidProperties } from '@arrirpc/schema/dist/adapters';
2
3
  import { OptionalKind } from '@sinclair/typebox';
3
4
  import { TypeCompiler } from '@sinclair/typebox/compiler';
4
5
  import { Value } from '@sinclair/typebox/value';
@@ -7,70 +8,64 @@ import { jsonSchemaToJtdSchema } from 'json-schema-to-atd';
7
8
  function typeboxAdapter(input) {
8
9
  const schema = jsonSchemaToJtdSchema(input);
9
10
  const compiled = TypeCompiler.Compile(input);
10
- const result = {
11
- ...schema,
12
- metadata: {
13
- id: input.$id ?? input.title,
14
- description: input.description,
15
- [SCHEMA_METADATA]: {
16
- _isAdapted: true,
17
- output: {},
18
- optional: input[OptionalKind] === "Optional",
19
- parse(val) {
20
- if (typeof val === "string") {
21
- const parsedVal = JSON.parse(val);
22
- if (compiled.Check(parsedVal)) {
23
- return parsedVal;
24
- }
25
- throw typeboxErrorsToArriError(
26
- compiled.Errors(parsedVal)
27
- );
28
- }
29
- if (compiled.Check(val)) {
30
- return val;
31
- }
32
- throw typeboxErrorsToArriError(compiled.Errors(val));
33
- },
34
- coerce(val) {
35
- return Value.Cast(input, val);
36
- },
37
- validate(val) {
38
- return compiled.Check(val);
39
- },
40
- serialize(val) {
41
- return compiled.Encode(val);
11
+ const validationMethods = {
12
+ output: {},
13
+ optional: input[OptionalKind] === "Optional",
14
+ parse(val, context) {
15
+ if (typeof val === "string") {
16
+ const parsedVal = JSON.parse(val);
17
+ if (compiled.Check(parsedVal)) {
18
+ return parsedVal;
42
19
  }
20
+ context.errors = typeboxErrorsToArriErrors(
21
+ compiled.Errors(parsedVal)
22
+ );
23
+ return undefined;
43
24
  }
25
+ if (compiled.Check(val)) {
26
+ return val;
27
+ }
28
+ context.errors = typeboxErrorsToArriErrors(compiled.Errors(val));
29
+ return undefined;
44
30
  },
45
- "~standard": {
46
- version: 1,
47
- vendor: "arri-typebox",
48
- validate(value) {
49
- if (compiled.Check(value)) {
50
- return {
51
- value
52
- };
53
- }
54
- if (typeof value === "string") {
55
- const parsedVal = JSON.parse(value);
56
- if (compiled.Check(parsedVal)) {
57
- return parsedVal;
58
- }
59
- }
60
- const errors = typeboxErrorsToArriError(compiled.Errors(value));
61
- return {
62
- issues: errors.errors.map((err) => ({
63
- message: err.message ?? "Unknown error",
64
- path: err.instancePath.split("/").filter((item) => item.length > 0)
65
- }))
66
- };
31
+ coerce(val, context) {
32
+ try {
33
+ return Value.Cast(input, val);
34
+ } catch (err) {
35
+ context.errors.push({
36
+ instancePath: "/",
37
+ message: err instanceof Error ? err.message : `${err}`
38
+ });
39
+ return undefined;
40
+ }
41
+ },
42
+ validate(val) {
43
+ return compiled.Check(val);
44
+ },
45
+ serialize(val, context) {
46
+ try {
47
+ return compiled.Encode(val);
48
+ } catch (err) {
49
+ context.errors.push({
50
+ instancePath: "/",
51
+ message: err instanceof Error ? err.message : `${err}`
52
+ });
53
+ return undefined;
67
54
  }
68
55
  }
69
56
  };
57
+ const result = {
58
+ ...schema,
59
+ metadata: {
60
+ id: input.$id ?? input.title,
61
+ description: input.description
62
+ },
63
+ [VALIDATOR_KEY]: validationMethods
64
+ };
70
65
  hideInvalidProperties(result);
71
66
  return result;
72
67
  }
73
- function typeboxErrorsToArriError(errs) {
68
+ function typeboxErrorsToArriErrors(errs) {
74
69
  const mappedErrs = [];
75
70
  for (const err of errs) {
76
71
  const obj = {
@@ -81,10 +76,7 @@ function typeboxErrorsToArriError(errs) {
81
76
  };
82
77
  mappedErrs.push(obj);
83
78
  }
84
- return new ValidationError({
85
- message: "Error validating input",
86
- errors: mappedErrs
87
- });
79
+ return mappedErrs;
88
80
  }
89
81
 
90
82
  export { typeboxAdapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/typebox-adapter",
3
- "version": "0.73.0",
3
+ "version": "0.74.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -22,11 +22,11 @@
22
22
  "directory": "languages/ts/ts-schema-typebox-adapter"
23
23
  },
24
24
  "dependencies": {
25
- "@arrirpc/schema": "0.73.0",
26
- "json-schema-to-atd": "0.73.0"
25
+ "@arrirpc/schema": "0.74.0",
26
+ "json-schema-to-atd": "0.74.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@arrirpc/server": "0.73.0"
29
+ "@arrirpc/server": "0.74.0"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "@sinclair/typebox": "^0.32"