morio_bridge 0.1.0 → 0.1.1

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,191 @@
1
+ import Validator from "fastest-validator";
2
+
3
+ export class MorioValidator {
4
+ private v: Validator | null = null;
5
+
6
+ constructor() {
7
+ this.v = new Validator({
8
+ useNewCustomCheckerFunction: true, // using new version
9
+ defaults: {
10
+ object: {
11
+ strict: "remove",
12
+ },
13
+ },
14
+ messages: {
15
+ // Register our new error message text
16
+ notEqual:
17
+ "The '{field}' field must not be equal to the '{otherField}' field",
18
+ },
19
+ aliases: {
20
+ notSame: {
21
+ type: "custom",
22
+ check: (
23
+ value: any,
24
+ errors: any,
25
+ schema: any,
26
+ field: string,
27
+ data: any
28
+ ) => {
29
+ // First run the base type validations (string, number, etc)
30
+ const baseSchema = { ...schema };
31
+ delete baseSchema.field; // Remove our custom field property
32
+ baseSchema.type = schema.baseType || "string"; // Use specified base type or default to string
33
+
34
+ // Create a validator just for the base type check
35
+ const baseValidator = new Validator();
36
+ const check = baseValidator.compile({ field: baseSchema });
37
+ const baseErrors = check({ field: value });
38
+
39
+ // If base validation failed, add those errors
40
+ if (Array.isArray(baseErrors)) {
41
+ // replace field key with the field key from the schema
42
+ const newErrors: any[] = baseErrors.map((error: any) => ({
43
+ ...error,
44
+ field,
45
+ message:
46
+ this.v?.messages[error.type]?.replace("{field}", field) ||
47
+ `validation failed for the '${field}' field`,
48
+ }));
49
+
50
+ // get errors message
51
+ errors.push(...newErrors);
52
+ }
53
+
54
+ // Then run our custom notEqual validation
55
+ const compareField = schema.field;
56
+ const compareValue = data[compareField];
57
+
58
+ if (value === compareValue) {
59
+ errors.push({
60
+ type: "notSame",
61
+ otherField: compareField,
62
+ message: `The ${field} field must not be tne same as the ${compareField} field`,
63
+ field,
64
+ });
65
+ }
66
+
67
+ if (errors.length > 0) {
68
+ return false;
69
+ }
70
+
71
+ return value;
72
+ },
73
+ },
74
+ same: {
75
+ type: "custom",
76
+ check: (
77
+ value: any,
78
+ errors: any,
79
+ schema: any,
80
+ field: string,
81
+ data: any
82
+ ) => {
83
+ // First run the base type validations (string, number, etc)
84
+ const baseSchema = { ...schema };
85
+ delete baseSchema.field; // Remove our custom field property
86
+ baseSchema.type = schema.baseType || "string"; // Use specified base type or default to string
87
+
88
+ // Create a validator just for the base type check
89
+ const baseValidator = new Validator();
90
+ const check = baseValidator.compile({ field: baseSchema });
91
+ const baseErrors = check({ field: value });
92
+
93
+ // If base validation failed, add those errors
94
+ if (Array.isArray(baseErrors)) {
95
+ // replace field key with the field key from the schema
96
+ const newErrors: any[] = baseErrors.map((error: any) => ({
97
+ ...error,
98
+ field,
99
+ message:
100
+ this.v?.messages[error.type]?.replace("{field}", field) ||
101
+ `validation failed for the '${field}' field`,
102
+ }));
103
+
104
+ // get errors message
105
+ errors.push(...newErrors);
106
+ }
107
+
108
+ // Then run our custom notEqual validation
109
+ const compareField = schema.field;
110
+ const compareValue = data[compareField];
111
+
112
+ if (value !== compareValue) {
113
+ errors.push({
114
+ type: "same",
115
+ otherField: compareField,
116
+ message: `The ${field} field must be equal to the ${compareField} field`,
117
+ field,
118
+ });
119
+ }
120
+
121
+ if (errors.length > 0) {
122
+ return false;
123
+ }
124
+
125
+ return value;
126
+ },
127
+ },
128
+ },
129
+ });
130
+ }
131
+
132
+ run(
133
+ options: {
134
+ schema: Record<string, any>;
135
+ data: Record<string, any>;
136
+ } = {
137
+ schema: {},
138
+ data: {},
139
+ }
140
+ ) {
141
+ try {
142
+ let { schema, data } = options;
143
+
144
+ if (!this.v) {
145
+ return {
146
+ from: "validator",
147
+ isValid: false,
148
+ errors: [
149
+ {
150
+ type: "internal_error",
151
+ message: "Validator not initialized",
152
+ },
153
+ ],
154
+ sanitized: {},
155
+ };
156
+ }
157
+
158
+ const check = this.v.compile(schema);
159
+ const errors = check(data);
160
+
161
+ if (errors instanceof Array && errors.length > 0) {
162
+ return {
163
+ from: "validator",
164
+ isValid: false,
165
+ errors,
166
+ sanitized: {},
167
+ };
168
+ }
169
+
170
+ return {
171
+ from: "validator",
172
+ isValid: true,
173
+ errors: [],
174
+ sanitized: data,
175
+ };
176
+ } catch (error: any) {
177
+ return {
178
+ from: "validator",
179
+ isValid: false,
180
+ errors: [
181
+ {
182
+ type: "internal_error",
183
+ message: "Validator not initialized",
184
+ stack: error.stack.split("\n"),
185
+ },
186
+ ],
187
+ sanitized: {},
188
+ };
189
+ }
190
+ }
191
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "ESNext",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": true,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
+
23
+ // Some stricter flags (disabled by default)
24
+ "noUnusedLocals": false,
25
+ "noUnusedParameters": false,
26
+ "noPropertyAccessFromIndexSignature": false
27
+ }
28
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morio_bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - r2g
@@ -21,6 +21,28 @@ files:
21
21
  - lib/morio_bridge.rb
22
22
  - lib/rubygems_plugin.rb
23
23
  - morio_bridge.gemspec
24
+ - server/bun.lock
25
+ - server/db/pg-dev/client/client.ts
26
+ - server/db/pg-dev/client/commonInputTypes.ts
27
+ - server/db/pg-dev/client/enums.ts
28
+ - server/db/pg-dev/client/index.ts
29
+ - server/db/pg-dev/client/internal/class.ts
30
+ - server/db/pg-dev/client/internal/prismaNamespace.ts
31
+ - server/db/pg-dev/client/models.ts
32
+ - server/db/pg-dev/client/models/User.ts
33
+ - server/db/pg-dev/schema.prisma
34
+ - server/examples/commands.ts
35
+ - server/examples/create-test.ts
36
+ - server/index.ts
37
+ - server/package.json
38
+ - server/plugins/index.ts
39
+ - server/plugins/lib/correct.schema.prisma
40
+ - server/plugins/lib/schema-converter.ts
41
+ - server/plugins/lib/schema.ts
42
+ - server/plugins/lib/util.ts
43
+ - server/plugins/orm.ts
44
+ - server/plugins/validator.ts
45
+ - server/tsconfig.json
24
46
  homepage: https://github.com/r2g/morio-bridge
25
47
  licenses:
26
48
  - MIT