schematox 0.3.0 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,20 @@
1
1
  # Changelog
2
2
 
3
- ## [0.3.0](https://github.com/incerta/schematox/compare/v0.2.0...v0.3.0) (2024-05-04)
3
+ ## [0.3.1](https://github.com/incerta/schematox/compare/v0.3.0...v0.3.1)
4
+
5
+ ### Bugfix
6
+
7
+ - [`e3527df`](https://github.com/incerta/schematox/commit/e3527dfb46b73a4b6579e3d2aea58f3301aded9a) [#24](https://github.com/incerta/schematox/pull/25) Parser should preserve object optional key only if it is specified in source
8
+
9
+ ### Chore
10
+
11
+ - [`7d98a81`](https://github.com/incerta/schematox/commit/7d98a81c2bc18280299365da32f8346d5b145560) Use "es2015" tsconfig target instead of outdated "es5"
12
+
13
+ ### Features
14
+
15
+ - [`8bc2082`](https://github.com/incerta/schematox/commit/8bc208211457901f4f7246f00694f112d56f8d56) [#22](https://github.com/incerta/schematox/issues/22) Ensure optional properties in schemas reflect as optional in object context (@incerta)
16
+
17
+ ## [0.3.0](https://github.com/incerta/schematox/compare/v0.2.0...v0.3.0)
4
18
 
5
19
  ### Features
6
20
 
package/dist/parse.js CHANGED
@@ -1,48 +1,11 @@
1
1
  "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
- if (ar || !(i in from)) {
21
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
- ar[i] = from[i];
23
- }
24
- }
25
- return to.concat(ar || Array.prototype.slice.call(from));
26
- };
27
- var __values = (this && this.__values) || function(o) {
28
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
29
- if (m) return m.call(o);
30
- if (o && typeof o.length === "number") return {
31
- next: function () {
32
- if (o && i >= o.length) o = void 0;
33
- return { value: o && o[i++], done: !o };
34
- }
35
- };
36
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.parse = void 0;
40
- var error_1 = require("./error");
41
- var fp_1 = require("./utils/fp");
42
- var verify_primitive_1 = require("./verify-primitive");
4
+ const error_1 = require("./error");
5
+ const fp_1 = require("./utils/fp");
6
+ const verify_primitive_1 = require("./verify-primitive");
43
7
  function parse(schema, subject) {
44
- var e_1, _a;
45
- var errors = [];
8
+ const errors = [];
46
9
  if (schema.optional === true && subject === undefined) {
47
10
  return (0, fp_1.right)(undefined);
48
11
  }
@@ -57,18 +20,32 @@ function parse(schema, subject) {
57
20
  {
58
21
  code: error_1.ERROR_CODE.invalidType,
59
22
  path: this || [],
60
- schema: schema,
61
- subject: subject,
23
+ schema,
24
+ subject,
62
25
  },
63
26
  ]);
64
27
  }
65
- var result = {};
66
- for (var key in schema.of) {
67
- var nestedSchema = schema.of[key];
68
- var nestedValue = subject[key];
69
- var parsed = parse.bind(__spreadArray(__spreadArray([], __read((this || [])), false), [key], false))(nestedSchema, nestedValue);
28
+ const result = {};
29
+ for (const key in schema.of) {
30
+ const narrowedSubj = subject;
31
+ const nestedSchema = schema.of[key];
32
+ if (Object.prototype.hasOwnProperty.call(narrowedSubj, key) === false) {
33
+ if (nestedSchema.optional !== true) {
34
+ return (0, fp_1.left)([
35
+ {
36
+ code: error_1.ERROR_CODE.invalidType,
37
+ path: [...(this || []), key],
38
+ schema,
39
+ subject,
40
+ },
41
+ ]);
42
+ }
43
+ continue;
44
+ }
45
+ const nestedValue = narrowedSubj[key];
46
+ const parsed = parse.bind([...(this || []), key])(nestedSchema, nestedValue);
70
47
  if (parsed.left) {
71
- parsed.left.forEach(function (err) { return errors.push(err); });
48
+ parsed.left.forEach((err) => errors.push(err));
72
49
  continue;
73
50
  }
74
51
  result[key] = parsed.right;
@@ -84,18 +61,18 @@ function parse(schema, subject) {
84
61
  {
85
62
  code: error_1.ERROR_CODE.invalidType,
86
63
  path: this || [],
87
- subject: subject,
88
- schema: schema,
64
+ subject,
65
+ schema,
89
66
  },
90
67
  ]);
91
68
  }
92
- var result = [];
93
- for (var i = 0; i < subject.length; i++) {
94
- var nestedSchema = schema.of;
95
- var nestedValue = subject[i];
96
- var parsed = parse.bind(__spreadArray(__spreadArray([], __read((this || [])), false), [i], false))(nestedSchema, nestedValue);
69
+ const result = [];
70
+ for (let i = 0; i < subject.length; i++) {
71
+ const nestedSchema = schema.of;
72
+ const nestedValue = subject[i];
73
+ const parsed = parse.bind([...(this || []), i])(nestedSchema, nestedValue);
97
74
  if (parsed.left) {
98
- parsed.left.forEach(function (err) { return errors.push(err); });
75
+ parsed.left.forEach((err) => errors.push(err));
99
76
  continue;
100
77
  }
101
78
  result.push(parsed.right);
@@ -109,8 +86,8 @@ function parse(schema, subject) {
109
86
  {
110
87
  code: error_1.ERROR_CODE.invalidRange,
111
88
  path: this || [],
112
- subject: subject,
113
- schema: schema,
89
+ subject,
90
+ schema,
114
91
  },
115
92
  ]);
116
93
  }
@@ -120,40 +97,30 @@ function parse(schema, subject) {
120
97
  {
121
98
  code: error_1.ERROR_CODE.invalidRange,
122
99
  path: this || [],
123
- subject: subject,
124
- schema: schema,
100
+ subject,
101
+ schema,
125
102
  },
126
103
  ]);
127
104
  }
128
105
  return (0, fp_1.right)(result);
129
106
  }
130
107
  if (schema.type === 'union') {
131
- try {
132
- for (var _b = __values(schema.of), _c = _b.next(); !_c.done; _c = _b.next()) {
133
- var subSchema = _c.value;
134
- var parsed = parse(subSchema, subject);
135
- if (parsed.left === undefined) {
136
- return (0, fp_1.right)(parsed.right);
137
- }
138
- }
139
- }
140
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
141
- finally {
142
- try {
143
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
108
+ for (const subSchema of schema.of) {
109
+ const parsed = parse(subSchema, subject);
110
+ if (parsed.left === undefined) {
111
+ return (0, fp_1.right)(parsed.right);
144
112
  }
145
- finally { if (e_1) throw e_1.error; }
146
113
  }
147
114
  return (0, fp_1.left)([
148
115
  {
149
116
  code: error_1.ERROR_CODE.invalidType,
150
117
  path: this || [],
151
- subject: subject,
152
- schema: schema,
118
+ subject,
119
+ schema,
153
120
  },
154
121
  ]);
155
122
  }
156
- var verified = (0, verify_primitive_1.verifyPrimitive)(schema, subject);
123
+ const verified = (0, verify_primitive_1.verifyPrimitive)(schema, subject);
157
124
  if (verified === true) {
158
125
  return (0, fp_1.right)(subject);
159
126
  }
@@ -161,8 +128,8 @@ function parse(schema, subject) {
161
128
  {
162
129
  code: verified,
163
130
  path: this || [],
164
- subject: subject,
165
- schema: schema,
131
+ subject,
132
+ schema,
166
133
  },
167
134
  ]);
168
135
  }
package/dist/struct.js CHANGED
@@ -1,72 +1,40 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __values = (this && this.__values) || function(o) {
14
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
15
- if (m) return m.call(o);
16
- if (o && typeof o.length === "number") return {
17
- next: function () {
18
- if (o && i >= o.length) o = void 0;
19
- return { value: o && o[i++], done: !o };
20
- }
21
- };
22
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
23
- };
24
2
  Object.defineProperty(exports, "__esModule", { value: true });
25
3
  exports.union = exports.array = exports.object = exports.literal = exports.boolean = exports.number = exports.string = exports.makeStruct = void 0;
26
- var constants_1 = require("./constants");
27
- var validate_1 = require("./validate");
28
- var parse_1 = require("./parse");
4
+ const constants_1 = require("./constants");
5
+ const validate_1 = require("./validate");
6
+ const parse_1 = require("./parse");
29
7
  function makeStruct(schema) {
30
- var params = constants_1.PARAMS_BY_SCHEMA_TYPE[schema.type];
31
- var result = {
8
+ const params = constants_1.PARAMS_BY_SCHEMA_TYPE[schema.type];
9
+ const result = {
32
10
  __schema: schema,
33
- parse: function (subj) { return (0, parse_1.parse)(schema, subj); },
34
- validate: function (subj) { return (0, validate_1.validate)(schema, subj); },
35
- guard: function (subj) {
36
- return (0, validate_1.validate)(schema, subj).left === undefined;
37
- },
11
+ parse: (subj) => (0, parse_1.parse)(schema, subj),
12
+ validate: (subj) => (0, validate_1.validate)(schema, subj),
13
+ guard: (subj) => (0, validate_1.validate)(schema, subj).left === undefined,
38
14
  };
39
15
  if (params.has('optional')) {
40
- result.optional = function () { return makeStruct(__assign(__assign({}, schema), { optional: true })); };
16
+ result.optional = () => makeStruct(Object.assign(Object.assign({}, schema), { optional: true }));
41
17
  }
42
18
  if (params.has('nullable')) {
43
- result.nullable = function () { return makeStruct(__assign(__assign({}, schema), { nullable: true })); };
19
+ result.nullable = () => makeStruct(Object.assign(Object.assign({}, schema), { nullable: true }));
44
20
  }
45
21
  if (params.has('description')) {
46
- result.description = function (description) {
47
- return makeStruct(__assign(__assign({}, schema), { description: description }));
48
- };
22
+ result.description = (description) => makeStruct(Object.assign(Object.assign({}, schema), { description }));
49
23
  }
50
24
  if (params.has('brand')) {
51
- result.brand = function (key, value) {
52
- return makeStruct(__assign(__assign({}, schema), { brand: [key, value] }));
53
- };
25
+ result.brand = (key, value) => makeStruct(Object.assign(Object.assign({}, schema), { brand: [key, value] }));
54
26
  }
55
27
  if (params.has('min')) {
56
- result.min = function (min) { return makeStruct(__assign(__assign({}, schema), { min: min })); };
28
+ result.min = (min) => makeStruct(Object.assign(Object.assign({}, schema), { min }));
57
29
  }
58
30
  if (params.has('max')) {
59
- result.max = function (max) { return makeStruct(__assign(__assign({}, schema), { max: max })); };
31
+ result.max = (max) => makeStruct(Object.assign(Object.assign({}, schema), { max }));
60
32
  }
61
33
  if (params.has('minLength')) {
62
- result.minLength = function (minLength) {
63
- return makeStruct(__assign(__assign({}, schema), { minLength: minLength }));
64
- };
34
+ result.minLength = (minLength) => makeStruct(Object.assign(Object.assign({}, schema), { minLength }));
65
35
  }
66
36
  if (params.has('maxLength')) {
67
- result.maxLength = function (maxLength) {
68
- return makeStruct(__assign(__assign({}, schema), { maxLength: maxLength }));
69
- };
37
+ result.maxLength = (maxLength) => makeStruct(Object.assign(Object.assign({}, schema), { maxLength }));
70
38
  }
71
39
  return result;
72
40
  }
@@ -84,37 +52,26 @@ function boolean() {
84
52
  }
85
53
  exports.boolean = boolean;
86
54
  function literal(of) {
87
- return makeStruct({ type: 'literal', of: of });
55
+ return makeStruct({ type: 'literal', of });
88
56
  }
89
57
  exports.literal = literal;
90
58
  function object(of) {
91
- var schema = { type: 'object', of: {} };
92
- for (var key in of) {
59
+ const schema = { type: 'object', of: {} };
60
+ for (const key in of) {
93
61
  schema.of[key] = of[key].__schema;
94
62
  }
95
63
  return makeStruct(schema);
96
64
  }
97
65
  exports.object = object;
98
66
  function array(of) {
99
- var schema = { type: 'array', of: of.__schema };
67
+ const schema = { type: 'array', of: of.__schema };
100
68
  return makeStruct(schema);
101
69
  }
102
70
  exports.array = array;
103
71
  function union(of) {
104
- var e_1, _a;
105
- var schema = { type: 'union', of: [] };
106
- try {
107
- for (var of_1 = __values(of), of_1_1 = of_1.next(); !of_1_1.done; of_1_1 = of_1.next()) {
108
- var subSchema = of_1_1.value;
109
- schema.of.push(subSchema.__schema);
110
- }
111
- }
112
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
113
- finally {
114
- try {
115
- if (of_1_1 && !of_1_1.done && (_a = of_1.return)) _a.call(of_1);
116
- }
117
- finally { if (e_1) throw e_1.error; }
72
+ const schema = { type: 'union', of: [] };
73
+ for (const subSchema of of) {
74
+ schema.of.push(subSchema.__schema);
118
75
  }
119
76
  return makeStruct(schema);
120
77
  }
package/dist/utils/fp.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.right = exports.left = void 0;
4
- var left = function (value) { return ({ left: value }); };
4
+ const left = (value) => ({ left: value });
5
5
  exports.left = left;
6
- var right = function (value) { return ({ right: value }); };
6
+ const right = (value) => ({ right: value });
7
7
  exports.right = right;
package/dist/validate.js CHANGED
@@ -1,48 +1,11 @@
1
1
  "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
- if (ar || !(i in from)) {
21
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
- ar[i] = from[i];
23
- }
24
- }
25
- return to.concat(ar || Array.prototype.slice.call(from));
26
- };
27
- var __values = (this && this.__values) || function(o) {
28
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
29
- if (m) return m.call(o);
30
- if (o && typeof o.length === "number") return {
31
- next: function () {
32
- if (o && i >= o.length) o = void 0;
33
- return { value: o && o[i++], done: !o };
34
- }
35
- };
36
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.guard = exports.validate = void 0;
40
- var fp_1 = require("./utils/fp");
41
- var error_1 = require("./error");
42
- var verify_primitive_1 = require("./verify-primitive");
4
+ const fp_1 = require("./utils/fp");
5
+ const error_1 = require("./error");
6
+ const verify_primitive_1 = require("./verify-primitive");
43
7
  function validate(schema, subject) {
44
- var e_1, _a;
45
- var errors = [];
8
+ const errors = [];
46
9
  if (schema.optional === true && subject === undefined) {
47
10
  return (0, fp_1.right)(undefined);
48
11
  }
@@ -57,19 +20,19 @@ function validate(schema, subject) {
57
20
  {
58
21
  code: error_1.ERROR_CODE.invalidType,
59
22
  path: this || [],
60
- schema: schema,
61
- subject: subject,
23
+ schema,
24
+ subject,
62
25
  },
63
26
  ]);
64
27
  }
65
- var subjectKeySet = new Set(Object.keys(subject));
66
- for (var key in schema.of) {
28
+ const subjectKeySet = new Set(Object.keys(subject));
29
+ for (const key in schema.of) {
67
30
  subjectKeySet.delete(key);
68
- var nestedValue = subject[key];
69
- var nestedSchema = schema.of[key];
70
- var validated = validate.bind(__spreadArray(__spreadArray([], __read((this || [])), false), [key], false))(nestedSchema, nestedValue);
31
+ const nestedValue = subject[key];
32
+ const nestedSchema = schema.of[key];
33
+ const validated = validate.bind([...(this || []), key])(nestedSchema, nestedValue);
71
34
  if (validated.left) {
72
- validated.left.forEach(function (err) { return errors.push(err); });
35
+ validated.left.forEach((err) => errors.push(err));
73
36
  continue;
74
37
  }
75
38
  }
@@ -79,8 +42,8 @@ function validate(schema, subject) {
79
42
  if (subjectKeySet.size !== 0) {
80
43
  errors.push({
81
44
  code: error_1.ERROR_CODE.invalidType,
82
- subject: subject,
83
- schema: schema,
45
+ subject,
46
+ schema,
84
47
  path: this || [],
85
48
  });
86
49
  return (0, fp_1.left)(errors);
@@ -93,17 +56,17 @@ function validate(schema, subject) {
93
56
  {
94
57
  code: error_1.ERROR_CODE.invalidType,
95
58
  path: this || [],
96
- subject: subject,
97
- schema: schema,
59
+ subject,
60
+ schema,
98
61
  },
99
62
  ]);
100
63
  }
101
- for (var i = 0; i < subject.length; i++) {
102
- var nestedSchema = schema.of;
103
- var nestedValue = subject[i];
104
- var validated = validate.bind(__spreadArray(__spreadArray([], __read((this || [])), false), [i], false))(nestedSchema, nestedValue);
64
+ for (let i = 0; i < subject.length; i++) {
65
+ const nestedSchema = schema.of;
66
+ const nestedValue = subject[i];
67
+ const validated = validate.bind([...(this || []), i])(nestedSchema, nestedValue);
105
68
  if (validated.left) {
106
- validated.left.forEach(function (err) { return errors.push(err); });
69
+ validated.left.forEach((err) => errors.push(err));
107
70
  continue;
108
71
  }
109
72
  }
@@ -116,8 +79,8 @@ function validate(schema, subject) {
116
79
  {
117
80
  code: error_1.ERROR_CODE.invalidRange,
118
81
  path: this || [],
119
- subject: subject,
120
- schema: schema,
82
+ subject,
83
+ schema,
121
84
  },
122
85
  ]);
123
86
  }
@@ -127,39 +90,29 @@ function validate(schema, subject) {
127
90
  {
128
91
  code: error_1.ERROR_CODE.invalidRange,
129
92
  path: this || [],
130
- subject: subject,
131
- schema: schema,
93
+ subject,
94
+ schema,
132
95
  },
133
96
  ]);
134
97
  }
135
98
  return (0, fp_1.right)(subject);
136
99
  }
137
100
  if (schema.type === 'union') {
138
- try {
139
- for (var _b = __values(schema.of), _c = _b.next(); !_c.done; _c = _b.next()) {
140
- var subSchema = _c.value;
141
- if (validate(subSchema, subject).left === undefined) {
142
- return (0, fp_1.right)(subject);
143
- }
144
- }
145
- }
146
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
147
- finally {
148
- try {
149
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
101
+ for (const subSchema of schema.of) {
102
+ if (validate(subSchema, subject).left === undefined) {
103
+ return (0, fp_1.right)(subject);
150
104
  }
151
- finally { if (e_1) throw e_1.error; }
152
105
  }
153
106
  return (0, fp_1.left)([
154
107
  {
155
108
  code: error_1.ERROR_CODE.invalidType,
156
109
  path: this || [],
157
- subject: subject,
158
- schema: schema,
110
+ subject,
111
+ schema,
159
112
  },
160
113
  ]);
161
114
  }
162
- var verified = (0, verify_primitive_1.verifyPrimitive)(schema, subject);
115
+ const verified = (0, verify_primitive_1.verifyPrimitive)(schema, subject);
163
116
  if (verified === true) {
164
117
  return (0, fp_1.right)(subject);
165
118
  }
@@ -167,8 +120,8 @@ function validate(schema, subject) {
167
120
  {
168
121
  code: verified,
169
122
  path: this || [],
170
- subject: subject,
171
- schema: schema,
123
+ subject,
124
+ schema,
172
125
  },
173
126
  ]);
174
127
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.verifyPrimitive = void 0;
4
- var error_1 = require("./error");
4
+ const error_1 = require("./error");
5
5
  function verifyPrimitive(schema, subject) {
6
6
  if (schema.optional === true && subject === undefined) {
7
7
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schematox",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Define JSON compatible schema statically/programmatically and parse/validate its subject with typesafety",
5
5
  "author": "Konstantin Mazur",
6
6
  "license": "MIT",