@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.
@@ -0,0 +1,96 @@
1
+ import { TypeValues, isSchemaFormElements, isSchemaFormEnum, isSchemaFormDiscriminator, isSchemaFormValues, isSchemaFormProperties, isSchemaFormRef } from '@arrirpc/type-defs';
2
+
3
+ function secretSymbol(key) {
4
+ return Symbol.for(key);
5
+ }
6
+ function newValidationContext(exitOnFirstError = false, maxDepth = 500) {
7
+ return {
8
+ instancePath: "",
9
+ schemaPath: "",
10
+ errors: [],
11
+ exitOnFirstError,
12
+ depth: 0,
13
+ maxDepth
14
+ };
15
+ }
16
+ const VALIDATOR_KEY = secretSymbol("arri/schema/validator/v1");
17
+ function isASchema(input) {
18
+ if (typeof input !== "object" || !input) {
19
+ return false;
20
+ }
21
+ if ("metadata" in input && typeof input.metadata === "object" && input.metadata) {
22
+ if ("id" in input.metadata && typeof input.metadata.id !== "string" && typeof input.metadata.id !== "undefined") {
23
+ return false;
24
+ }
25
+ if ("description" in input.metadata && typeof input.metadata.description !== "string" && typeof input.metadata.description !== "undefined") {
26
+ return false;
27
+ }
28
+ if ("isDeprecated" in input.metadata && typeof input.metadata.isDeprecated !== "boolean" && typeof input.metadata.isDeprecated !== "undefined") {
29
+ return false;
30
+ }
31
+ }
32
+ return VALIDATOR_KEY in input;
33
+ }
34
+ function isAScalarSchema(input) {
35
+ return isASchema(input) && "type" in input && typeof input.type === "string" && (TypeValues.includes(input.type) || NumberTypeValues.includes(input.type));
36
+ }
37
+ const NumberTypeValues = [
38
+ "float32",
39
+ "float64",
40
+ "int8",
41
+ "int16",
42
+ "int32",
43
+ "int64",
44
+ "uint8",
45
+ "uint16",
46
+ "uint32",
47
+ "uint64"
48
+ ];
49
+ function isAAraySchema(input) {
50
+ return isASchema(input) && isSchemaFormElements(input);
51
+ }
52
+ function isAStringEnumSchema(input) {
53
+ return isASchema(input) && isSchemaFormEnum(input);
54
+ }
55
+ function isADiscriminatorSchema(input) {
56
+ return isASchema(input) && isSchemaFormDiscriminator(input);
57
+ }
58
+ function isARecordSchema(input) {
59
+ return isASchema(input) && isSchemaFormValues(input);
60
+ }
61
+ function isAObjectSchema(input) {
62
+ return isASchema(input) && isSchemaFormProperties(input);
63
+ }
64
+ function isObject(input) {
65
+ return typeof input === "object" && input !== null;
66
+ }
67
+ function isARefSchema(input) {
68
+ return isASchema(input) && isSchemaFormRef(input);
69
+ }
70
+
71
+ function createStandardSchemaProperty(validate, parse, vendor = "arri") {
72
+ return {
73
+ version: 1,
74
+ vendor,
75
+ validate(input) {
76
+ const ctx = newValidationContext(true);
77
+ const result = parse(input, ctx);
78
+ if (ctx.errors.length) {
79
+ return {
80
+ issues: ctx.errors.map((err) => ({
81
+ message: err.message ?? "Unknown error",
82
+ path: err.instancePath?.split("/").filter((val) => val.length > 0)
83
+ }))
84
+ };
85
+ }
86
+ return {
87
+ value: result
88
+ };
89
+ }
90
+ };
91
+ }
92
+ function hideInvalidProperties(schema) {
93
+ Object.defineProperty(schema, "~standard", { enumerable: false });
94
+ }
95
+
96
+ export { NumberTypeValues as N, VALIDATOR_KEY as V, isAObjectSchema as a, isAStringEnumSchema as b, isAAraySchema as c, isARecordSchema as d, isADiscriminatorSchema as e, isARefSchema as f, createStandardSchemaProperty as g, hideInvalidProperties as h, isAScalarSchema as i, isObject as j, isASchema as k, newValidationContext as n };
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ const typeDefs = require('@arrirpc/type-defs');
4
+
5
+ function secretSymbol(key) {
6
+ return Symbol.for(key);
7
+ }
8
+ function newValidationContext(exitOnFirstError = false, maxDepth = 500) {
9
+ return {
10
+ instancePath: "",
11
+ schemaPath: "",
12
+ errors: [],
13
+ exitOnFirstError,
14
+ depth: 0,
15
+ maxDepth
16
+ };
17
+ }
18
+ const VALIDATOR_KEY = secretSymbol("arri/schema/validator/v1");
19
+ function isASchema(input) {
20
+ if (typeof input !== "object" || !input) {
21
+ return false;
22
+ }
23
+ if ("metadata" in input && typeof input.metadata === "object" && input.metadata) {
24
+ if ("id" in input.metadata && typeof input.metadata.id !== "string" && typeof input.metadata.id !== "undefined") {
25
+ return false;
26
+ }
27
+ if ("description" in input.metadata && typeof input.metadata.description !== "string" && typeof input.metadata.description !== "undefined") {
28
+ return false;
29
+ }
30
+ if ("isDeprecated" in input.metadata && typeof input.metadata.isDeprecated !== "boolean" && typeof input.metadata.isDeprecated !== "undefined") {
31
+ return false;
32
+ }
33
+ }
34
+ return VALIDATOR_KEY in input;
35
+ }
36
+ function isAScalarSchema(input) {
37
+ return isASchema(input) && "type" in input && typeof input.type === "string" && (typeDefs.TypeValues.includes(input.type) || NumberTypeValues.includes(input.type));
38
+ }
39
+ const NumberTypeValues = [
40
+ "float32",
41
+ "float64",
42
+ "int8",
43
+ "int16",
44
+ "int32",
45
+ "int64",
46
+ "uint8",
47
+ "uint16",
48
+ "uint32",
49
+ "uint64"
50
+ ];
51
+ function isAAraySchema(input) {
52
+ return isASchema(input) && typeDefs.isSchemaFormElements(input);
53
+ }
54
+ function isAStringEnumSchema(input) {
55
+ return isASchema(input) && typeDefs.isSchemaFormEnum(input);
56
+ }
57
+ function isADiscriminatorSchema(input) {
58
+ return isASchema(input) && typeDefs.isSchemaFormDiscriminator(input);
59
+ }
60
+ function isARecordSchema(input) {
61
+ return isASchema(input) && typeDefs.isSchemaFormValues(input);
62
+ }
63
+ function isAObjectSchema(input) {
64
+ return isASchema(input) && typeDefs.isSchemaFormProperties(input);
65
+ }
66
+ function isObject(input) {
67
+ return typeof input === "object" && input !== null;
68
+ }
69
+ function isARefSchema(input) {
70
+ return isASchema(input) && typeDefs.isSchemaFormRef(input);
71
+ }
72
+
73
+ function createStandardSchemaProperty(validate, parse, vendor = "arri") {
74
+ return {
75
+ version: 1,
76
+ vendor,
77
+ validate(input) {
78
+ const ctx = newValidationContext(true);
79
+ const result = parse(input, ctx);
80
+ if (ctx.errors.length) {
81
+ return {
82
+ issues: ctx.errors.map((err) => ({
83
+ message: err.message ?? "Unknown error",
84
+ path: err.instancePath?.split("/").filter((val) => val.length > 0)
85
+ }))
86
+ };
87
+ }
88
+ return {
89
+ value: result
90
+ };
91
+ }
92
+ };
93
+ }
94
+ function hideInvalidProperties(schema) {
95
+ Object.defineProperty(schema, "~standard", { enumerable: false });
96
+ }
97
+
98
+ exports.NumberTypeValues = NumberTypeValues;
99
+ exports.VALIDATOR_KEY = VALIDATOR_KEY;
100
+ exports.createStandardSchemaProperty = createStandardSchemaProperty;
101
+ exports.hideInvalidProperties = hideInvalidProperties;
102
+ exports.isAAraySchema = isAAraySchema;
103
+ exports.isADiscriminatorSchema = isADiscriminatorSchema;
104
+ exports.isAObjectSchema = isAObjectSchema;
105
+ exports.isARecordSchema = isARecordSchema;
106
+ exports.isARefSchema = isARefSchema;
107
+ exports.isAScalarSchema = isAScalarSchema;
108
+ exports.isASchema = isASchema;
109
+ exports.isAStringEnumSchema = isAStringEnumSchema;
110
+ exports.isObject = isObject;
111
+ exports.newValidationContext = newValidationContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/schema",
3
- "version": "0.73.0",
3
+ "version": "0.74.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -22,10 +22,10 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
+ "@standard-schema/spec": "1.0.0",
25
26
  "scule": "^1.3.0",
26
27
  "uncrypto": "^0.1.3",
27
- "@standard-schema/spec": "1.0.0-rc.0",
28
- "@arrirpc/type-defs": "0.73.0"
28
+ "@arrirpc/type-defs": "0.74.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "ajv": "^8.17.1",