schiva 0.1.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/src/schema.js ADDED
@@ -0,0 +1,230 @@
1
+ const { ObjType, ValidateType } = require('./types');
2
+
3
+ // Declared keys are read as own properties only: {}.toString or {}.constructor must not count as present.
4
+ // A value read from a plain object is its own unless Object.prototype has the key, which avoids the slower
5
+ // own-property check in the common case. The prototype is read with __proto__ rather than Object.getPrototypeOf(),
6
+ // which makes V8 deoptimize the code around it (twice slower). Objects without that accessor (no prototype, or an
7
+ // own "__proto__" key from JSON.parse) are not taken as plain and get the own-property check.
8
+ function ownValue(obj, key) {
9
+ const value = obj[key];
10
+ // eslint-disable-next-line no-proto -- see above
11
+ if (value === undefined || (obj.__proto__ === Object.prototype && !(key in Object.prototype))) {
12
+ return value;
13
+ }
14
+ return Object.prototype.hasOwnProperty.call(obj, key) ? value : undefined;
15
+ }
16
+
17
+ class Schema {
18
+ constructor(schema = {}, options = {}) {
19
+ this.schema = schema;
20
+ this.options = options;
21
+ this.isOpen = options.isOpen === undefined ? true : options.isOpen;
22
+ this.isMandatory = options.isMandatory === undefined ? true : options.isMandatory;
23
+ this.isNullable = options.isNullable === undefined ? false : options.isNullable;
24
+ // Type that keys not declared in the schema must satisfy (only used when the schema is open).
25
+ this.additionalType = options.additionalType;
26
+ // [{ pattern, type }]: keys matching a pattern must satisfy its type, and are not checked by additionalType.
27
+ this.patternTypes = options.patternTypes || [];
28
+ this.minProperties = options.minProperties;
29
+ this.maxProperties = options.maxProperties;
30
+ // [{ key, required: [properties] } or { key, type }]: when key is present, the properties must be present too,
31
+ // or the whole object must satisfy type.
32
+ this.dependencies = options.dependencies || [];
33
+ // Type every key must satisfy, reported as "Key <name>".
34
+ this.propertyNameType = options.propertyNameType;
35
+ this.visitObjs();
36
+ this.keys = Object.keys(this.schema);
37
+ this.keySet = new Set(this.keys);
38
+ }
39
+
40
+ visitObjs() {
41
+ // Nested schemas share the options, except the ones about the keys of this object.
42
+ const options = { ...this.options, patternTypes: undefined, dependencies: undefined, propertyNameType: undefined };
43
+ const keys = Object.keys(this.schema);
44
+ for (let i = 0; i < keys.length; i += 1) {
45
+ const key = keys[i];
46
+ const value = this.schema[key];
47
+ if (!(value instanceof Schema)) {
48
+ if (!(value instanceof ValidateType)) {
49
+ const obj = new ObjType({ schema: value });
50
+ this.schema[key] = new Schema(obj.schema, options);
51
+ } else if (value instanceof ObjType) {
52
+ this.schema[key] = new Schema(value.schema, options);
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ // Fast boolean check equivalent to validate(obj).length === 0 that builds no messages.
59
+ isValid(obj) {
60
+ if (obj === undefined) {
61
+ return !this.isMandatory;
62
+ }
63
+ if (obj === null) {
64
+ return this.isNullable;
65
+ }
66
+ if (typeof obj !== 'object' || Array.isArray(obj)) {
67
+ return false;
68
+ }
69
+ const { keys, keySet } = this;
70
+ for (let i = 0; i < keys.length; i += 1) {
71
+ const key = keys[i];
72
+ if (!this.schema[key].isValid(ownValue(obj, key))) {
73
+ return false;
74
+ }
75
+ }
76
+ const objKeys = Object.keys(obj);
77
+ if (
78
+ (this.minProperties !== undefined && objKeys.length < this.minProperties) ||
79
+ (this.maxProperties !== undefined && objKeys.length > this.maxProperties)
80
+ ) {
81
+ return false;
82
+ }
83
+ const { patternTypes, propertyNameType } = this;
84
+ if (!this.isOpen || this.additionalType || patternTypes.length > 0 || propertyNameType) {
85
+ for (let i = 0; i < objKeys.length; i += 1) {
86
+ const key = objKeys[i];
87
+ if (propertyNameType && !propertyNameType.isValid(key)) {
88
+ return false;
89
+ }
90
+ let matched = false;
91
+ for (let j = 0; j < patternTypes.length; j += 1) {
92
+ if (patternTypes[j].pattern.test(key)) {
93
+ matched = true;
94
+ if (!patternTypes[j].type.isValid(obj[key])) {
95
+ return false;
96
+ }
97
+ }
98
+ }
99
+ if (!keySet.has(key) && !matched) {
100
+ if (!this.isOpen || (this.additionalType && !this.additionalType.isValid(obj[key]))) {
101
+ return false;
102
+ }
103
+ }
104
+ }
105
+ }
106
+ return this.dependencies.every(
107
+ ({ key, required, type }) =>
108
+ ownValue(obj, key) === undefined ||
109
+ (required ? required.every((property) => ownValue(obj, property) !== undefined) : type.isValid(obj))
110
+ );
111
+ }
112
+
113
+ validate(obj, fieldName = undefined) {
114
+ return this.isValid(obj) ? [] : this.errors(obj, fieldName);
115
+ }
116
+
117
+ // Compiles the schema into generated code, several times faster than validate(): see compileType() in compile.js
118
+ // for the options. The compiled function does not see changes made to the schema afterwards.
119
+ compile(options = {}) {
120
+ // eslint-disable-next-line global-require -- compile.js requires this module
121
+ return require('./compile').compileType(this, options);
122
+ }
123
+
124
+ // Error messages of a value already known to be invalid.
125
+ errors(obj, fieldName = undefined) {
126
+ const name = fieldName || 'Value';
127
+ const { keys: schemaKeys, keySet } = this;
128
+ const errors = [];
129
+ if (obj === undefined) {
130
+ if (this.isMandatory) {
131
+ errors.push(`${name} is mandatory`);
132
+ }
133
+ return errors;
134
+ }
135
+ if (obj === null) {
136
+ if (!this.isNullable) {
137
+ errors.push(`${name} cannot be null`);
138
+ }
139
+ return errors;
140
+ }
141
+ if (typeof obj !== 'object' || Array.isArray(obj)) {
142
+ errors.push(`${name} must be an object`);
143
+ return errors;
144
+ }
145
+ for (let i = 0; i < schemaKeys.length; i += 1) {
146
+ const key = schemaKeys[i];
147
+ const type = this.schema[key];
148
+ const value = ownValue(obj, key);
149
+ if (!type.isValid(value)) {
150
+ errors.push(type.errors(value, fieldName ? `${fieldName}.${key}` : key));
151
+ }
152
+ }
153
+ const objKeys = Object.keys(obj);
154
+ for (let i = 0; i < objKeys.length; i += 1) {
155
+ const key = objKeys[i];
156
+ const keyName = fieldName ? `${fieldName}.${key}` : key;
157
+ if (this.propertyNameType && !this.propertyNameType.isValid(key)) {
158
+ errors.push(this.propertyNameType.errors(key, `Key ${keyName}`));
159
+ }
160
+ let matched = false;
161
+ this.patternTypes.forEach(({ pattern, type }) => {
162
+ if (pattern.test(key)) {
163
+ matched = true;
164
+ if (!type.isValid(obj[key])) {
165
+ errors.push(type.errors(obj[key], keyName));
166
+ }
167
+ }
168
+ });
169
+ if (!keySet.has(key) && !matched) {
170
+ if (!this.isOpen) {
171
+ errors.push(`Unexpected key: ${keyName}`);
172
+ } else if (this.additionalType && !this.additionalType.isValid(obj[key])) {
173
+ errors.push(this.additionalType.errors(obj[key], keyName));
174
+ }
175
+ }
176
+ }
177
+ if (this.minProperties !== undefined && objKeys.length < this.minProperties) {
178
+ errors.push(`${name} must have at least ${this.minProperties} properties`);
179
+ }
180
+ if (this.maxProperties !== undefined && objKeys.length > this.maxProperties) {
181
+ errors.push(`${name} must have at most ${this.maxProperties} properties`);
182
+ }
183
+ this.dependencies.forEach(({ key, required, type }) => {
184
+ if (ownValue(obj, key) === undefined) {
185
+ return;
186
+ }
187
+ const keyName = fieldName ? `${fieldName}.${key}` : key;
188
+ if (required) {
189
+ required
190
+ .filter((property) => ownValue(obj, property) === undefined)
191
+ .forEach((property) => {
192
+ const propertyName = fieldName ? `${fieldName}.${property}` : property;
193
+ errors.push(`${propertyName} is mandatory when ${keyName} is present`);
194
+ });
195
+ } else if (!type.isValid(obj)) {
196
+ errors.push(type.errors(obj, fieldName));
197
+ }
198
+ });
199
+ return errors.flat(Infinity);
200
+ }
201
+
202
+ mandatory(isMandatory = true) {
203
+ this.isMandatory = isMandatory;
204
+ return this;
205
+ }
206
+
207
+ nullable(isNullable = true) {
208
+ this.isNullable = isNullable;
209
+ return this;
210
+ }
211
+
212
+ optional() {
213
+ this.isMandatory = false;
214
+ return this;
215
+ }
216
+
217
+ required() {
218
+ this.isMandatory = true;
219
+ return this;
220
+ }
221
+
222
+ notNull() {
223
+ this.isNullable = false;
224
+ return this;
225
+ }
226
+ }
227
+
228
+ module.exports = {
229
+ Schema,
230
+ };
@@ -0,0 +1,62 @@
1
+ const { ValidateType } = require('./validate-type');
2
+
3
+ // Value must satisfy every type; reports the errors of the first type that fails.
4
+ class AllOfType extends ValidateType {
5
+ constructor(options = {}) {
6
+ super(options);
7
+ this.types = options.types || [];
8
+ }
9
+
10
+ validate(value, fieldName = 'Value') {
11
+ const result = super.validate(value, fieldName);
12
+ if (result) {
13
+ return result;
14
+ }
15
+ if (value !== undefined && value !== null) {
16
+ for (let i = 0; i < this.types.length; i += 1) {
17
+ if (!this.types[i].isValid(value)) {
18
+ return this.types[i].errors(value, fieldName);
19
+ }
20
+ }
21
+ }
22
+ return undefined;
23
+ }
24
+
25
+ isValid(value) {
26
+ const presence = this.checkPresence(value);
27
+ if (presence !== undefined) {
28
+ return presence;
29
+ }
30
+ for (let i = 0; i < this.types.length; i += 1) {
31
+ if (!this.types[i].isValid(value)) {
32
+ return false;
33
+ }
34
+ }
35
+ return true;
36
+ }
37
+ }
38
+
39
+ function AllOf(options) {
40
+ return new AllOfType(options);
41
+ }
42
+
43
+ function allOf(types, isMandatory = true, isNullable = false) {
44
+ if (types !== undefined && types !== null && !Array.isArray(types) && typeof types === 'object') {
45
+ return new AllOfType(types);
46
+ }
47
+ return new AllOfType({ types, isMandatory, isNullable });
48
+ }
49
+
50
+ function oallOf(types, isMandatory = false, isNullable = false) {
51
+ if (types !== undefined && types !== null && !Array.isArray(types) && typeof types === 'object') {
52
+ return new AllOfType({ isMandatory: false, ...types });
53
+ }
54
+ return new AllOfType({ types, isMandatory, isNullable });
55
+ }
56
+
57
+ module.exports = {
58
+ AllOfType,
59
+ AllOf,
60
+ allOf,
61
+ oallOf,
62
+ };
@@ -0,0 +1,63 @@
1
+ const { ValidateType } = require('./validate-type');
2
+
3
+ class AnyOfType extends ValidateType {
4
+ constructor(options = {}) {
5
+ super(options);
6
+ this.types = options.types;
7
+ }
8
+
9
+ validate(value, fieldName = 'Value') {
10
+ const result = super.validate(value, fieldName);
11
+ if (result) {
12
+ return result;
13
+ }
14
+ if (value !== undefined && value !== null) {
15
+ if (this.isValid(value)) {
16
+ return undefined;
17
+ }
18
+ return this.types.map((type) => type.errors(value, fieldName));
19
+ }
20
+ return undefined;
21
+ }
22
+
23
+ isValid(value) {
24
+ const presence = this.checkPresence(value);
25
+ if (presence !== undefined) {
26
+ return presence;
27
+ }
28
+ if (!this.types || this.types.length === 0) {
29
+ return true;
30
+ }
31
+ for (let i = 0; i < this.types.length; i += 1) {
32
+ if (this.types[i].isValid(value)) {
33
+ return true;
34
+ }
35
+ }
36
+ return false;
37
+ }
38
+ }
39
+
40
+ function AnyOf(options) {
41
+ return new AnyOfType(options);
42
+ }
43
+
44
+ function anyOf(types, isMandatory = true, isNullable = false) {
45
+ if (types !== undefined && types !== null && !Array.isArray(types) && typeof types === 'object') {
46
+ return new AnyOfType(types);
47
+ }
48
+ return new AnyOfType({ types, isMandatory, isNullable });
49
+ }
50
+
51
+ function oanyOf(types, isMandatory = false, isNullable = false) {
52
+ if (types !== undefined && types !== null && !Array.isArray(types) && typeof types === 'object') {
53
+ return new AnyOfType({ isMandatory: false, ...types });
54
+ }
55
+ return new AnyOfType({ types, isMandatory, isNullable });
56
+ }
57
+
58
+ module.exports = {
59
+ AnyOfType,
60
+ AnyOf,
61
+ anyOf,
62
+ oanyOf,
63
+ };
@@ -0,0 +1,32 @@
1
+ const { ValidateType } = require('./validate-type');
2
+
3
+ class AnyType extends ValidateType {
4
+ isValid(value) {
5
+ return this.checkPresence(value) ?? true;
6
+ }
7
+ }
8
+
9
+ function Any(options) {
10
+ return new AnyType(options);
11
+ }
12
+
13
+ function any(isMandatory = true, isNullable = false) {
14
+ if (isMandatory !== undefined && isMandatory !== null && typeof isMandatory === 'object') {
15
+ return new AnyType(isMandatory);
16
+ }
17
+ return new AnyType({ isMandatory, isNullable });
18
+ }
19
+
20
+ function oany(isMandatory = false, isNullable = false) {
21
+ if (isMandatory !== undefined && isMandatory !== null && typeof isMandatory === 'object') {
22
+ return new AnyType({ isMandatory: false, ...isMandatory });
23
+ }
24
+ return new AnyType({ isMandatory, isNullable });
25
+ }
26
+
27
+ module.exports = {
28
+ AnyType,
29
+ Any,
30
+ any,
31
+ oany,
32
+ };
@@ -0,0 +1,137 @@
1
+ const { hasDuplicates } = require('./has-duplicates');
2
+ const { ValidateType } = require('./validate-type');
3
+
4
+ class ArrayOfType extends ValidateType {
5
+ constructor(options = {}) {
6
+ super(options);
7
+ // A type for every element, or an array of types for the elements at each position (a tuple).
8
+ this.type = options.type;
9
+ this.min = options.min;
10
+ this.max = options.max;
11
+ this.unique = options.unique;
12
+ // At least one element must satisfy it.
13
+ this.contains = options.contains;
14
+ // With a tuple, the elements after its last position must satisfy it.
15
+ this.additionalType = options.additionalType;
16
+ }
17
+
18
+ hasMatch(value) {
19
+ for (let i = 0; i < value.length; i += 1) {
20
+ if (this.contains.isValid(value[i])) {
21
+ return true;
22
+ }
23
+ }
24
+ return false;
25
+ }
26
+
27
+ validate(value, fieldName = 'Value') {
28
+ const result = super.validate(value, fieldName);
29
+ if (result) {
30
+ return result;
31
+ }
32
+ if (value !== undefined && value !== null) {
33
+ if (!Array.isArray(value)) {
34
+ return `${fieldName} must be an array`;
35
+ }
36
+ if (this.min !== undefined && value.length < this.min) {
37
+ return `${fieldName} must have at least ${this.min} elements`;
38
+ }
39
+ if (this.max !== undefined && value.length > this.max) {
40
+ return `${fieldName} must have at most ${this.max} elements`;
41
+ }
42
+ if (this.unique && hasDuplicates(value)) {
43
+ return `${fieldName} must not have duplicate elements`;
44
+ }
45
+ if (this.contains && !this.hasMatch(value)) {
46
+ return `${fieldName} must contain at least one matching element`;
47
+ }
48
+ if (this.type) {
49
+ const errors = [];
50
+ const check = (type, i) => {
51
+ const item = value[i];
52
+ if (!type.isValid(item)) {
53
+ errors.push(type.errors(item, `${fieldName}[${i}]`));
54
+ }
55
+ };
56
+ if (Array.isArray(this.type)) {
57
+ for (let i = 0; i < this.type.length; i += 1) {
58
+ check(this.type[i], i);
59
+ }
60
+ if (this.additionalType) {
61
+ for (let i = this.type.length; i < value.length; i += 1) {
62
+ check(this.additionalType, i);
63
+ }
64
+ }
65
+ } else {
66
+ for (let i = 0; i < value.length; i += 1) {
67
+ check(this.type, i);
68
+ }
69
+ }
70
+ return errors.flat();
71
+ }
72
+ }
73
+ return undefined;
74
+ }
75
+
76
+ isValid(value) {
77
+ const presence = this.checkPresence(value);
78
+ if (presence !== undefined) {
79
+ return presence;
80
+ }
81
+ if (
82
+ !Array.isArray(value) ||
83
+ (this.min !== undefined && value.length < this.min) ||
84
+ (this.max !== undefined && value.length > this.max) ||
85
+ (this.unique && hasDuplicates(value)) ||
86
+ (this.contains && !this.hasMatch(value))
87
+ ) {
88
+ return false;
89
+ }
90
+ if (Array.isArray(this.type)) {
91
+ for (let i = 0; i < this.type.length; i += 1) {
92
+ if (!this.type[i].isValid(value[i])) {
93
+ return false;
94
+ }
95
+ }
96
+ if (this.additionalType) {
97
+ for (let i = this.type.length; i < value.length; i += 1) {
98
+ if (!this.additionalType.isValid(value[i])) {
99
+ return false;
100
+ }
101
+ }
102
+ }
103
+ } else if (this.type) {
104
+ for (let i = 0; i < value.length; i += 1) {
105
+ if (!this.type.isValid(value[i])) {
106
+ return false;
107
+ }
108
+ }
109
+ }
110
+ return true;
111
+ }
112
+ }
113
+
114
+ function ArrayOf(options) {
115
+ return new ArrayOfType(options);
116
+ }
117
+
118
+ function arrOf(type, min, max, isMandatory = true, isNullable = false) {
119
+ if (type !== undefined && type !== null && typeof type === 'object') {
120
+ return new ArrayOfType(type);
121
+ }
122
+ return new ArrayOfType({ type, min, max, isMandatory, isNullable });
123
+ }
124
+
125
+ function oarrOf(type, min, max, isMandatory = false, isNullable = false) {
126
+ if (type !== undefined && type !== null && typeof type === 'object') {
127
+ return new ArrayOfType({ isMandatory: false, ...type });
128
+ }
129
+ return new ArrayOfType({ type, min, max, isMandatory, isNullable });
130
+ }
131
+
132
+ module.exports = {
133
+ ArrayOfType,
134
+ ArrayOf,
135
+ arrOf,
136
+ oarrOf,
137
+ };
@@ -0,0 +1,43 @@
1
+ const { ValidateType } = require('./validate-type');
2
+
3
+ class BooleanType extends ValidateType {
4
+ validate(value, fieldName = 'Value') {
5
+ const result = super.validate(value, fieldName);
6
+ if (result) {
7
+ return result;
8
+ }
9
+ if (value !== undefined && value !== null && typeof value !== 'boolean') {
10
+ return `${fieldName} must be a boolean`;
11
+ }
12
+ return undefined;
13
+ }
14
+
15
+ isValid(value) {
16
+ return this.checkPresence(value) ?? typeof value === 'boolean';
17
+ }
18
+ }
19
+
20
+ function Boolean(options) {
21
+ return new BooleanType(options);
22
+ }
23
+
24
+ function bool(isMandatory = true, isNullable = false) {
25
+ if (isMandatory !== undefined && isMandatory !== null && typeof isMandatory === 'object') {
26
+ return new BooleanType(isMandatory);
27
+ }
28
+ return new BooleanType({ isMandatory, isNullable });
29
+ }
30
+
31
+ function obool(isMandatory = false, isNullable = false) {
32
+ if (isMandatory !== undefined && isMandatory !== null && typeof isMandatory === 'object') {
33
+ return new BooleanType({ isMandatory: false, ...isMandatory });
34
+ }
35
+ return new BooleanType({ isMandatory, isNullable });
36
+ }
37
+
38
+ module.exports = {
39
+ BooleanType,
40
+ Boolean,
41
+ bool,
42
+ obool,
43
+ };
@@ -0,0 +1,32 @@
1
+ // Length in Unicode code points, as JSON Schema counts it: a surrogate pair is one character. Same as [...value].length
2
+ // without building an array.
3
+ function codePointLength(value) {
4
+ let count = 0;
5
+ for (let i = 0; i < value.length; i += 1) {
6
+ const code = value.charCodeAt(i);
7
+ if (code >= 0xd800 && code <= 0xdbff && i + 1 < value.length) {
8
+ const next = value.charCodeAt(i + 1);
9
+ if (next >= 0xdc00 && next <= 0xdfff) {
10
+ i += 1;
11
+ }
12
+ }
13
+ count += 1;
14
+ }
15
+ return count;
16
+ }
17
+
18
+ // A string has between length / 2 and length code points, so the UTF-16 length decides the comparison unless it is
19
+ // close to the limit; only then are code points counted.
20
+ function hasFewerCodePoints(value, min) {
21
+ return value.length < min || (value.length < 2 * min && codePointLength(value) < min);
22
+ }
23
+
24
+ function hasMoreCodePoints(value, max) {
25
+ return value.length > 2 * max || (value.length > max && codePointLength(value) > max);
26
+ }
27
+
28
+ module.exports = {
29
+ codePointLength,
30
+ hasFewerCodePoints,
31
+ hasMoreCodePoints,
32
+ };
@@ -0,0 +1,48 @@
1
+ const { ValidateType } = require('./validate-type');
2
+
3
+ // When the value satisfies `ifType` it must satisfy `thenType`, otherwise `elseType`; a missing branch accepts
4
+ // anything. Only the errors of the branch are reported, like JSON Schema if/then/else.
5
+ class ConditionalType extends ValidateType {
6
+ constructor(options = {}) {
7
+ super(options);
8
+ this.ifType = options.ifType;
9
+ this.thenType = options.thenType;
10
+ this.elseType = options.elseType;
11
+ }
12
+
13
+ branch(value) {
14
+ return this.ifType.isValid(value) ? this.thenType : this.elseType;
15
+ }
16
+
17
+ validate(value, fieldName = 'Value') {
18
+ const result = super.validate(value, fieldName);
19
+ if (result) {
20
+ return result;
21
+ }
22
+ if (value !== undefined && value !== null) {
23
+ const branch = this.branch(value);
24
+ if (branch && !branch.isValid(value)) {
25
+ return branch.errors(value, fieldName);
26
+ }
27
+ }
28
+ return undefined;
29
+ }
30
+
31
+ isValid(value) {
32
+ const presence = this.checkPresence(value);
33
+ if (presence !== undefined) {
34
+ return presence;
35
+ }
36
+ const branch = this.branch(value);
37
+ return !branch || branch.isValid(value);
38
+ }
39
+ }
40
+
41
+ function Conditional(options) {
42
+ return new ConditionalType(options);
43
+ }
44
+
45
+ module.exports = {
46
+ ConditionalType,
47
+ Conditional,
48
+ };
@@ -0,0 +1,51 @@
1
+ const { StringType } = require('./string');
2
+
3
+ class EnumType extends StringType {
4
+ constructor(options = {}) {
5
+ super(options);
6
+ this.options = options.options;
7
+ }
8
+
9
+ validate(value, fieldName = 'Value') {
10
+ const result = super.validate(value, fieldName);
11
+ if (result) {
12
+ return result;
13
+ }
14
+ if (value !== undefined && value !== null) {
15
+ if (!this.options.includes(value)) {
16
+ return `${fieldName} must be one of: ${this.options.join(', ')}`;
17
+ }
18
+ }
19
+ return undefined;
20
+ }
21
+
22
+ isValid(value) {
23
+ return super.isValid(value) && (value === undefined || value === null || this.options.includes(value));
24
+ }
25
+ }
26
+
27
+ function Enum(options) {
28
+ return new EnumType(options);
29
+ }
30
+
31
+ function enumt(options, isMandatory = true, isNullable = false) {
32
+ if (options !== undefined && options !== null && typeof options === 'object') {
33
+ return new EnumType(options);
34
+ }
35
+ return new EnumType({ options, isMandatory, isNullable });
36
+ }
37
+
38
+ function oenumt(options, isMandatory = false, isNullable = false) {
39
+ if (options !== undefined && options !== null && typeof options === 'object') {
40
+ return new EnumType({ isMandatory: false, ...options });
41
+ }
42
+ return new EnumType({ options, isMandatory, isNullable });
43
+ }
44
+
45
+ module.exports = {
46
+ EnumType,
47
+ Enum,
48
+ enumt,
49
+ oenumt,
50
+ oenum: oenumt,
51
+ };