@naturalcycles/nodejs-lib 15.44.0 → 15.45.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.
@@ -2,7 +2,7 @@ import { _lazyValue } from '@naturalcycles/js-lib';
2
2
  import { Set2 } from '@naturalcycles/js-lib/object';
3
3
  import { _substringAfterLast } from '@naturalcycles/js-lib/string';
4
4
  import { _, Ajv } from 'ajv';
5
- import { validTLDs } from './tlds.js';
5
+ import { validTLDs } from '../tlds.js';
6
6
  /* eslint-disable @typescript-eslint/prefer-string-starts-ends-with */
7
7
  // oxlint-disable unicorn/prefer-code-point
8
8
  const AJV_OPTIONS = {
@@ -1,5 +1,5 @@
1
1
  import type { Set2 } from '@naturalcycles/js-lib/object';
2
- import { type AnyObject, type IANATimezone, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
2
+ import { type AnyObject, type IANATimezone, type Inclusiveness, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type StringMap, type UnixTimestamp, type UnixTimestampMillis } from '@naturalcycles/js-lib/types';
3
3
  export declare const j: {
4
4
  string(): JsonSchemaStringBuilder<string, string, false>;
5
5
  number(): JsonSchemaNumberBuilder<number, number, false>;
@@ -111,10 +111,11 @@ export declare class JsonSchemaNumberBuilder<IN extends number = number, OUT = I
111
111
  exclusiveMin(exclusiveMinimum: number): this;
112
112
  max(maximum: number): this;
113
113
  exclusiveMax(exclusiveMaximum: number): this;
114
- /**
115
- * Both ranges are inclusive.
116
- */
117
- range(minimum: number, maximum: number): this;
114
+ lessThan(value: number): this;
115
+ lessThanOrEqual(value: number): this;
116
+ moreThan(value: number): this;
117
+ moreThanOrEqual(value: number): this;
118
+ range(minimum: number, maximum: number, incl: Inclusiveness): this;
118
119
  int32(): this;
119
120
  int64(): this;
120
121
  float(): this;
@@ -163,7 +164,7 @@ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<strin
163
164
  /**
164
165
  * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
165
166
  */
166
- dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in keyof PROPS | "id" | "created" | "updated"]: K extends "id" | "created" | "updated" ? {
167
+ dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in "id" | keyof PROPS | "created" | "updated"]: K extends "id" | "created" | "updated" ? {
167
168
  id: JsonSchemaStringBuilder<string, string, false>;
168
169
  created: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
169
170
  updated: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
@@ -264,6 +265,17 @@ type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> =
264
265
  [K in keyof B]: B[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never;
265
266
  }[number];
266
267
  interface JsonBuilderRuleOpt {
268
+ /**
269
+ * Text of error message to return when the validation fails for the given rule:
270
+ *
271
+ * `{ msg: "is not a valid Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
272
+ */
267
273
  msg?: string;
274
+ /**
275
+ * A friendly name for what we are validating, that will be used in error messages:
276
+ *
277
+ * `{ name: "Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
278
+ */
279
+ name?: string;
268
280
  }
269
281
  export {};
@@ -5,6 +5,7 @@ import { _uniq } from '@naturalcycles/js-lib/array';
5
5
  import { _assert } from '@naturalcycles/js-lib/error';
6
6
  import { _deepCopy, _sortObject } from '@naturalcycles/js-lib/object';
7
7
  import { JWT_REGEX, } from '@naturalcycles/js-lib/types';
8
+ import { TIMEZONES } from '../timezones.js';
8
9
  import { JSON_SCHEMA_ORDER, mergeJsonSchemaObjects } from './jsonSchemaBuilder.util.js';
9
10
  export const j = {
10
11
  string() {
@@ -177,6 +178,8 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
177
178
  return this.pattern(pattern.source, opt);
178
179
  }
179
180
  pattern(pattern, opt) {
181
+ if (opt?.name)
182
+ this.setErrorMessage('pattern', `is not a valid ${opt.name}`);
180
183
  if (opt?.msg)
181
184
  this.setErrorMessage('pattern', opt.msg);
182
185
  Object.assign(this.schema, { pattern });
@@ -283,9 +286,7 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
283
286
  */
284
287
  ianaTimezone() {
285
288
  // UTC is added to assist unit-testing, which uses UTC by default (not technically a valid Iana timezone identifier)
286
- return j
287
- .enum([...Intl.supportedValuesOf('timeZone'), 'UTC'], { msg: 'is an invalid IANA timezone' })
288
- .branded();
289
+ return j.enum(TIMEZONES, { msg: 'is an invalid IANA timezone' }).branded();
289
290
  }
290
291
  }
291
292
  export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
@@ -321,12 +322,23 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
321
322
  Object.assign(this.schema, { exclusiveMaximum });
322
323
  return this;
323
324
  }
324
- /**
325
- * Both ranges are inclusive.
326
- */
327
- range(minimum, maximum) {
328
- Object.assign(this.schema, { minimum, maximum });
329
- return this;
325
+ lessThan(value) {
326
+ return this.exclusiveMax(value);
327
+ }
328
+ lessThanOrEqual(value) {
329
+ return this.max(value);
330
+ }
331
+ moreThan(value) {
332
+ return this.exclusiveMin(value);
333
+ }
334
+ moreThanOrEqual(value) {
335
+ return this.min(value);
336
+ }
337
+ range(minimum, maximum, incl) {
338
+ if (incl === '[)') {
339
+ return this.moreThanOrEqual(minimum).lessThan(maximum);
340
+ }
341
+ return this.moreThanOrEqual(minimum).lessThanOrEqual(maximum);
330
342
  }
331
343
  int32() {
332
344
  const MIN_INT32 = -(2 ** 31);
@@ -534,6 +546,8 @@ export class JsonSchemaBufferBuilder extends JsonSchemaAnyBuilder {
534
546
  export class JsonSchemaEnumBuilder extends JsonSchemaAnyBuilder {
535
547
  constructor(enumValues, opt) {
536
548
  super({ enum: enumValues });
549
+ if (opt?.name)
550
+ this.setErrorMessage('pattern', `is not a valid ${opt.name}`);
537
551
  if (opt?.msg)
538
552
  this.setErrorMessage('enum', opt.msg);
539
553
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A complicated merge of timezones from the underlying Javascript engine
3
+ * and a list exported from Wikipedia.
4
+ *
5
+ * Why? The `Int.supportedValuesOf('timeZone')` we use provide only
6
+ * canonical/standardized timezone values like `Europe/Stockholm`
7
+ * but does not list accepted aliases like `ETC/Gmt`, `CET` or `Canada/Atlantic`.
8
+ */
9
+ export declare const TIMEZONES: any;