@cleverbrush/schema 1.1.2 → 1.1.4

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.
@@ -3,7 +3,8 @@ type StringSchemaBuilderCreateProps<T = string, R extends boolean = true> = Part
3
3
  /**
4
4
  * Allows to define a schema for a string. It can be: required or optional,
5
5
  * restricted to be equal to a certain value, restricted to have a certain
6
- * length.
6
+ * length, restricted to start with a certain value, restricted to end with
7
+ * a certain value, restricted to match a certain regular expression.
7
8
  *
8
9
  * **NOTE** this class is exported only to give opportunity to extend it
9
10
  * by inheriting. It is not recommended to create an instance of this class
@@ -70,6 +71,18 @@ export declare class StringSchemaBuilder<TResult = string, TRequired extends boo
70
71
  * If set, restrict object to be equal to a certain value.
71
72
  */
72
73
  equalsTo: string | undefined;
74
+ /**
75
+ * If set, restrict string to start with a certain value.
76
+ */
77
+ startsWith: string | undefined;
78
+ /**
79
+ * If set, restrict string to end with a certain value.
80
+ */
81
+ endsWith: string | undefined;
82
+ /**
83
+ * If set, restrict string to match a certain regular expression.
84
+ */
85
+ matches: RegExp | undefined;
73
86
  /**
74
87
  * Array of preprocessor functions
75
88
  */
@@ -129,6 +142,30 @@ export declare class StringSchemaBuilder<TResult = string, TRequired extends boo
129
142
  * cancel `maxLength()` call.
130
143
  */
131
144
  clearMaxLength(): StringSchemaBuilder<TResult, TRequired>;
145
+ /**
146
+ * Restricts string to start with `val`.
147
+ */
148
+ startsWith(val: string): StringSchemaBuilder<TResult, TRequired>;
149
+ /**
150
+ * Cancels `startsWith()` call.
151
+ */
152
+ clearStartsWith(): StringSchemaBuilder<TResult, TRequired>;
153
+ /**
154
+ * Restricts string to end with `val`.
155
+ */
156
+ endsWith(val: string): StringSchemaBuilder<TResult, TRequired>;
157
+ /**
158
+ * Cancels `endsWith()` call.
159
+ */
160
+ clearEndsWith(): StringSchemaBuilder<TResult, TRequired>;
161
+ /**
162
+ * Restricts string to match `regexp`.
163
+ */
164
+ matches(regexp: RegExp): StringSchemaBuilder<TResult, TRequired>;
165
+ /**
166
+ * Cancels `matches()` call.
167
+ */
168
+ clearMatches(): StringSchemaBuilder<TResult, TRequired>;
132
169
  }
133
170
  /**
134
171
  * Creates a string schema restricted to be equal to `equals`.
@@ -2,7 +2,8 @@ import { SchemaBuilder } from './SchemaBuilder.js';
2
2
  /**
3
3
  * Allows to define a schema for a string. It can be: required or optional,
4
4
  * restricted to be equal to a certain value, restricted to have a certain
5
- * length.
5
+ * length, restricted to start with a certain value, restricted to end with
6
+ * a certain value, restricted to match a certain regular expression.
6
7
  *
7
8
  * **NOTE** this class is exported only to give opportunity to extend it
8
9
  * by inheriting. It is not recommended to create an instance of this class
@@ -56,6 +57,9 @@ export class StringSchemaBuilder extends SchemaBuilder {
56
57
  #minLength;
57
58
  #maxLength;
58
59
  #equalsTo;
60
+ #startsWith;
61
+ #endsWith;
62
+ #matches;
59
63
  static create(props) {
60
64
  return new StringSchemaBuilder({
61
65
  type: 'string',
@@ -74,6 +78,16 @@ export class StringSchemaBuilder extends SchemaBuilder {
74
78
  typeof props.equalsTo === 'undefined') {
75
79
  this.#equalsTo = props.equalsTo;
76
80
  }
81
+ if (typeof props.startsWith === 'string' &&
82
+ props.startsWith.length > 0) {
83
+ this.#startsWith = props.startsWith;
84
+ }
85
+ if (typeof props.endsWith === 'string' && props.endsWith.length > 0) {
86
+ this.#endsWith = props.endsWith;
87
+ }
88
+ if (props.matches instanceof RegExp) {
89
+ this.#matches = props.matches;
90
+ }
77
91
  }
78
92
  introspect() {
79
93
  return {
@@ -90,6 +104,18 @@ export class StringSchemaBuilder extends SchemaBuilder {
90
104
  * If set, restrict object to be equal to a certain value.
91
105
  */
92
106
  equalsTo: this.#equalsTo,
107
+ /**
108
+ * If set, restrict string to start with a certain value.
109
+ */
110
+ startsWith: this.#startsWith,
111
+ /**
112
+ * If set, restrict string to end with a certain value.
113
+ */
114
+ endsWith: this.#endsWith,
115
+ /**
116
+ * If set, restrict string to match a certain regular expression.
117
+ */
118
+ matches: this.#matches,
93
119
  /**
94
120
  * Array of preprocessor functions
95
121
  */
@@ -161,6 +187,32 @@ export class StringSchemaBuilder extends SchemaBuilder {
161
187
  ]
162
188
  };
163
189
  }
190
+ if (typeof this.#startsWith === 'string' &&
191
+ this.#startsWith.length > 0 &&
192
+ !objToValidate.startsWith(this.#startsWith)) {
193
+ return {
194
+ valid: false,
195
+ errors: [
196
+ {
197
+ message: `is expected to start with '${this.#startsWith}'`,
198
+ path: path
199
+ }
200
+ ]
201
+ };
202
+ }
203
+ if (typeof this.#endsWith === 'string' &&
204
+ this.#endsWith.length > 0 &&
205
+ !objToValidate.endsWith(this.#endsWith)) {
206
+ return {
207
+ valid: false,
208
+ errors: [
209
+ {
210
+ message: `is expected to end with '${this.#endsWith}'`,
211
+ path: path
212
+ }
213
+ ]
214
+ };
215
+ }
164
216
  if (typeof this.#minLength !== 'undefined') {
165
217
  if (objToValidate.length < this.#minLength)
166
218
  return {
@@ -185,6 +237,19 @@ export class StringSchemaBuilder extends SchemaBuilder {
185
237
  ]
186
238
  };
187
239
  }
240
+ if (this.#matches instanceof RegExp) {
241
+ if (!this.#matches.test(objToValidate)) {
242
+ return {
243
+ valid: false,
244
+ errors: [
245
+ {
246
+ message: `does not match to ${this.#matches}`,
247
+ path: path
248
+ }
249
+ ]
250
+ };
251
+ }
252
+ }
188
253
  return {
189
254
  valid: true,
190
255
  object: objToValidate
@@ -269,6 +334,69 @@ export class StringSchemaBuilder extends SchemaBuilder {
269
334
  ...schema
270
335
  });
271
336
  }
337
+ /**
338
+ * Restricts string to start with `val`.
339
+ */
340
+ startsWith(val) {
341
+ if (typeof val !== 'string' || !val)
342
+ throw new Error('non empty string expected');
343
+ return this.createFromProps({
344
+ ...this.introspect(),
345
+ startsWith: val
346
+ });
347
+ }
348
+ /**
349
+ * Cancels `startsWith()` call.
350
+ */
351
+ clearStartsWith() {
352
+ const schema = this.introspect();
353
+ delete schema.startsWith;
354
+ return this.createFromProps({
355
+ ...schema
356
+ });
357
+ }
358
+ /**
359
+ * Restricts string to end with `val`.
360
+ */
361
+ endsWith(val) {
362
+ if (typeof val !== 'string' || !val)
363
+ throw new Error('non empty string expected');
364
+ return this.createFromProps({
365
+ ...this.introspect(),
366
+ endsWith: val
367
+ });
368
+ }
369
+ /**
370
+ * Cancels `endsWith()` call.
371
+ */
372
+ clearEndsWith() {
373
+ const schema = this.introspect();
374
+ delete schema.endsWith;
375
+ return this.createFromProps({
376
+ ...schema
377
+ });
378
+ }
379
+ /**
380
+ * Restricts string to match `regexp`.
381
+ */
382
+ matches(regexp) {
383
+ if (!(regexp instanceof RegExp))
384
+ throw new Error('regexp expected');
385
+ return this.createFromProps({
386
+ ...this.introspect(),
387
+ matches: regexp
388
+ });
389
+ }
390
+ /**
391
+ * Cancels `matches()` call.
392
+ */
393
+ clearMatches() {
394
+ const schema = this.introspect();
395
+ delete schema.matches;
396
+ return this.createFromProps({
397
+ ...schema
398
+ });
399
+ }
272
400
  }
273
401
  /**
274
402
  * Creates a string schema.
package/package.json CHANGED
@@ -35,8 +35,8 @@
35
35
  "displayName": "Schema Definition And Validation",
36
36
  "tsconfig": "./tsconfig.json"
37
37
  },
38
- "version": "1.1.2",
38
+ "version": "1.1.4",
39
39
  "devDependencies": {
40
- "@cleverbrush/deep": "1.1.2"
40
+ "@cleverbrush/deep": "1.1.4"
41
41
  }
42
42
  }