@naturalcycles/nodejs-lib 15.95.0 → 15.96.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.
@@ -1,6 +1,7 @@
1
1
  import type { ValidationFunction, ValidationFunctionResult } from '@naturalcycles/js-lib';
2
2
  import type { Set2 } from '@naturalcycles/js-lib/object';
3
3
  import type { AnyObject, BaseDBEntity, IANATimezone, Inclusiveness, IsoDate, IsoDateTime, IsoMonth, NumberEnum, StringEnum, StringMap, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
4
+ import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec';
4
5
  import type { Ajv } from 'ajv';
5
6
  import { AjvValidationError } from './ajvValidationError.js';
6
7
  export declare const j: {
@@ -122,7 +123,7 @@ export declare const HIDDEN_AJV_SCHEMA: unique symbol;
122
123
  export type WithCachedAjvSchema<Base, OUT> = Base & {
123
124
  [HIDDEN_AJV_SCHEMA]: AjvSchema<OUT>;
124
125
  };
125
- export declare class JSchema<OUT, Opt> {
126
+ export declare class JSchema<OUT, Opt> implements StandardSchemaV1<unknown, OUT>, StandardJSONSchemaV1<unknown, OUT> {
126
127
  protected [HIDDEN_AJV_SCHEMA]: AjvSchema<any> | undefined;
127
128
  protected schema: JsonSchema;
128
129
  private _cfg?;
@@ -142,6 +143,7 @@ export declare class JSchema<OUT, Opt> {
142
143
  build(): JsonSchema<OUT>;
143
144
  clone(): this;
144
145
  cloneAndUpdateSchema(schema: Partial<JsonSchema>): this;
146
+ get ['~standard'](): StandardSchemaV1.Props<unknown, OUT> & StandardJSONSchemaV1.Props<unknown, OUT>;
145
147
  validate(input: unknown, opt?: AjvValidationOptions): OUT;
146
148
  isValid(input: unknown, opt?: AjvValidationOptions): boolean;
147
149
  getValidationResult(input: unknown, opt?: AjvValidationOptions): ValidationFunctionResult<OUT, AjvValidationError>;
@@ -161,6 +163,8 @@ export declare class JSchema<OUT, Opt> {
161
163
  */
162
164
  out: OUT;
163
165
  opt: Opt;
166
+ /** Forces OUT to be invariant (prevents covariant subtype matching in object property constraints). */
167
+ protected _invariantOut: (x: OUT) => void;
164
168
  }
165
169
  export declare class JBuilder<OUT, Opt> extends JSchema<OUT, Opt> {
166
170
  protected setErrorMessage(ruleName: string, errorMessage: string | undefined): void;
@@ -295,6 +295,26 @@ export class JSchema {
295
295
  _objectAssign(clone.schema, schema);
296
296
  return clone;
297
297
  }
298
+ get ['~standard']() {
299
+ const value = {
300
+ version: 1,
301
+ vendor: 'j',
302
+ validate: v => {
303
+ const [err, output] = this.getValidationResult(v);
304
+ if (err) {
305
+ // todo: make getValidationResult return issues with path, so we can pass the path here too
306
+ return { issues: [{ message: err.message }] };
307
+ }
308
+ return { value: output };
309
+ },
310
+ jsonSchema: {
311
+ input: () => this.build(),
312
+ output: () => this.build(),
313
+ },
314
+ };
315
+ Object.defineProperty(this, '~standard', { value });
316
+ return value;
317
+ }
298
318
  validate(input, opt) {
299
319
  const [err, output] = this.getValidationResult(input, opt);
300
320
  if (err)
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.95.0",
4
+ "version": "15.96.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
+ "@standard-schema/spec": "^1",
7
8
  "@types/js-yaml": "^4",
8
9
  "@types/jsonwebtoken": "^9",
9
10
  "@types/yargs": "^16",
@@ -29,6 +29,7 @@ import type {
29
29
  UnixTimestampMillis,
30
30
  } from '@naturalcycles/js-lib/types'
31
31
  import { _objectAssign, _typeCast, JWT_REGEX } from '@naturalcycles/js-lib/types'
32
+ import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec'
32
33
  import type { Ajv, ErrorObject } from 'ajv'
33
34
  import { _inspect } from '../../string/inspect.js'
34
35
  import {
@@ -339,7 +340,9 @@ export type WithCachedAjvSchema<Base, OUT> = Base & {
339
340
  With `Opt`, we can infer it as `{ foo?: string | undefined }`.
340
341
  */
341
342
 
342
- export class JSchema<OUT, Opt> {
343
+ export class JSchema<OUT, Opt>
344
+ implements StandardSchemaV1<unknown, OUT>, StandardJSONSchemaV1<unknown, OUT>
345
+ {
343
346
  protected [HIDDEN_AJV_SCHEMA]: AjvSchema<any> | undefined
344
347
  protected schema: JsonSchema
345
348
  private _cfg?: { ajv?: Ajv; inputName?: string }
@@ -426,6 +429,28 @@ export class JSchema<OUT, Opt> {
426
429
  return clone
427
430
  }
428
431
 
432
+ get ['~standard'](): StandardSchemaV1.Props<unknown, OUT> &
433
+ StandardJSONSchemaV1.Props<unknown, OUT> {
434
+ const value: StandardSchemaV1.Props<unknown, OUT> & StandardJSONSchemaV1.Props<unknown, OUT> = {
435
+ version: 1,
436
+ vendor: 'j',
437
+ validate: v => {
438
+ const [err, output] = this.getValidationResult(v)
439
+ if (err) {
440
+ // todo: make getValidationResult return issues with path, so we can pass the path here too
441
+ return { issues: [{ message: err.message }] }
442
+ }
443
+ return { value: output }
444
+ },
445
+ jsonSchema: {
446
+ input: () => this.build() as Record<string, unknown>,
447
+ output: () => this.build() as Record<string, unknown>,
448
+ },
449
+ }
450
+ Object.defineProperty(this, '~standard', { value })
451
+ return value
452
+ }
453
+
429
454
  validate(input: unknown, opt?: AjvValidationOptions): OUT {
430
455
  const [err, output] = this.getValidationResult(input, opt)
431
456
  if (err) throw err
@@ -481,6 +506,9 @@ export class JSchema<OUT, Opt> {
481
506
  */
482
507
  out!: OUT
483
508
  opt!: Opt
509
+
510
+ /** Forces OUT to be invariant (prevents covariant subtype matching in object property constraints). */
511
+ declare protected _invariantOut: (x: OUT) => void
484
512
  }
485
513
 
486
514
  // ==== JBuilder (chainable base) ====