bootpress 6.0.1 → 6.0.2

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.
@@ -7,6 +7,8 @@ type TypeMap = {
7
7
  "boolean[]": boolean[],
8
8
  "number": number,
9
9
  "number[]": number[],
10
+ "integer": number,
11
+ "integer[]": number[]
10
12
  }
11
13
 
12
14
  type ValidTypeKeys = keyof TypeMap;
package/helpers/index.js CHANGED
@@ -34,9 +34,9 @@ function asBoolean(o, errorMessage = undefined, errorStatus = 400) {
34
34
  return validBooleanStrings.get(lowercased);
35
35
  } else if (typeof o === "boolean") {
36
36
  return o;
37
- } else if (typeof o === "number"){
38
- if(o === 1) return true;
39
- if(o === 0) return false;
37
+ } else if (typeof o === "number") {
38
+ if (o === 1) return true;
39
+ if (o === 0) return false;
40
40
  }
41
41
  throw new HttpError(errorStatus, errorMessage);
42
42
  }
@@ -90,7 +90,7 @@ function asSchema(o, schema) {
90
90
  }
91
91
  }
92
92
  const expectedType = schemaKeyValues[i][1];
93
- const errorMessage = o[key] == null ? `Value of ${key} should have been a ${expectedType} but it's null` : `Value of ${key} should have been a ${expectedType} but it's a ${typeof o[key]}`;
93
+ // const errorMessage = o[key] == null ? `Value of ${key} should have been a ${expectedType} but it's null` : `Value of ${key} should have been a ${expectedType} but it's a ${typeof o[key]}`;
94
94
 
95
95
  if (typeof expectedType === "object") {
96
96
  if (Array.isArray(expectedType)) {
@@ -103,20 +103,7 @@ function asSchema(o, schema) {
103
103
  }
104
104
  }
105
105
  else if (typeof expectedType === "string") {
106
- if (expectedType.endsWith("[]")) {
107
- const elementType = expectedType.replace("[]", "");
108
- for (let j = 0; j < o[key].length; j++) {
109
- if (typeof o[key][j] !== elementType) {
110
- throw new HttpError(400, `Each element of ${key} should have been a ${elementType} but a ${typeof o[key][j]} is present (${o[key][j]})`);
111
- }
112
- }
113
- result[key] = o[key];
114
- }
115
- else if (typeof o[key] === expectedType) {
116
- result[key] = o[key];
117
- } else {
118
- throw new HttpError(400, errorMessage);
119
- }
106
+ result[key] = as(o[key], expectedType, key);
120
107
  }
121
108
  else {
122
109
  throw new HttpError(500, `Type of a schema key should be a primitive type or another schema`);
@@ -133,9 +120,9 @@ function schema(schema) {
133
120
  function asArrayOf(o, elementType) {
134
121
  if (Array.isArray(o)) {
135
122
  for (let i = 0; i < o.length; i++) {
136
- if(elementType === "integer"){
123
+ if (elementType === "integer") {
137
124
  asInteger(o[i]);
138
- }else if(typeof o[i] != elementType){
125
+ } else if (typeof o[i] != elementType) {
139
126
  throw new HttpError(400, `Each element in array should have been a ${elementType} but ${o[i]} is present with type ${typeof o[i]}`);
140
127
  }
141
128
  }
@@ -145,49 +132,50 @@ function asArrayOf(o, elementType) {
145
132
  }
146
133
  }
147
134
 
148
- function as(o, type) {
135
+ function as(o, type, namedErrorVariable = o) {
149
136
  if (typeof type == "string") {
150
137
  if (type.endsWith("[]")) {
151
138
  // array check
152
139
  const elementType = type.replace("[]", "");
153
140
  return asArrayOf(o, elementType);
154
- } else {
155
- if(type.endsWith("?") && o == null){
141
+ } else { // non array types:
142
+ if (type.endsWith("?") && o == null) {
156
143
  return null;
157
- }else if(type.endsWith("?") && o != null ){
144
+ } else if (type.endsWith("?") && o != null) {
158
145
  const actualType = type.replace("?", "");
159
146
  return as(o, actualType);
160
147
  }
161
148
  // primitive check
162
149
  switch (type) {
163
150
  case "string":
164
- return asString(o);
151
+ return asString(o, `Type of ${namedErrorVariable} should have been a string but it's ${o == null ? "null" : typeof o}`);
165
152
  case "number":
166
- return asNumber(o);
153
+ return asNumber(o, `Type of ${namedErrorVariable} should have been a number but it's ${o == null ? "null" : typeof o}`);
167
154
  case "boolean":
168
- return asBoolean(o);
155
+ return asBoolean(o, `Type of ${namedErrorVariable} should have been a boolean but it's ${o == null ? "null" : typeof o}`);
169
156
  case "integer":
170
- return asInteger(o);
157
+ return asInteger(o, `Type of ${namedErrorVariable} should have been an integer but it's ${o == null ? "null" : typeof o}`);
171
158
  default:
172
159
  throw new HttpError(500, `Unsupported type ${type}`);
173
160
  }
174
161
  }
175
162
  } else if (typeof type == "object" && type != null) {
176
163
  if (Array.isArray(type)) {
177
- if(type.length > 1){
164
+ if (type.length > 1) {
178
165
  throw new HttpError(500, `You can define only one schema for types ArrayOf<Schema>`);
179
- }else if (type.length < 1){
166
+ } else if (type.length < 1) {
180
167
  throw new HttpError(500, `You must define a schema for types ArrayOf<Schema>`);
181
168
  }
182
169
  // array schema validation
183
- if(!Array.isArray(o)){
170
+ if (!Array.isArray(o)) {
184
171
  throw new HttpError(400, `Provided value should have been an array. (${JSON.stringify(o)})`)
185
172
  }
186
173
  const providedSchema = type[0];
187
- for(let i = 0; i < o.length; i++){
188
- asSchema(o[i], providedSchema);
174
+ let result = [];
175
+ for (let i = 0; i < o.length; i++) {
176
+ result.push(asSchema(o[i], providedSchema));
189
177
  }
190
- return o;
178
+ return result;
191
179
  } else {
192
180
  // schema validation
193
181
  return asSchema(o, type);
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "bootpress",
3
- "version": "6.0.1",
4
- "description": "REST service methods for express",
5
- "main": "index.js",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/ufukbakan/bootpress.git"
9
- },
10
- "keywords": [
11
- "bootpress",
12
- "express",
13
- "rest",
14
- "service",
15
- "methods",
16
- "spring",
17
- "boot",
18
- "like"
19
- ],
20
- "author": "Ufuk Bakan",
21
- "license": "MIT",
22
- "bugs": {
23
- "url": "https://github.com/ufukbakan/bootpress/issues"
24
- },
25
- "homepage": "https://github.com/ufukbakan/bootpress#readme",
26
- "dependencies": {
27
- "express": "^4.18.2"
28
- },
29
- "devDependencies": {
30
- "@types/express": "^4.17.17",
31
- "typescript": "^4.9.5"
32
- }
33
- }
1
+ {
2
+ "name": "bootpress",
3
+ "version": "6.0.2",
4
+ "description": "REST service methods for express",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/ufukbakan/bootpress.git"
9
+ },
10
+ "keywords": [
11
+ "bootpress",
12
+ "express",
13
+ "rest",
14
+ "service",
15
+ "methods",
16
+ "spring",
17
+ "boot",
18
+ "like"
19
+ ],
20
+ "author": "Ufuk Bakan",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/ufukbakan/bootpress/issues"
24
+ },
25
+ "homepage": "https://github.com/ufukbakan/bootpress#readme",
26
+ "dependencies": {
27
+ "express": "^4.18.2"
28
+ },
29
+ "devDependencies": {
30
+ "@types/express": "^4.17.17",
31
+ "typescript": "^4.9.5"
32
+ }
33
+ }