@naturalcycles/nodejs-lib 15.94.0 → 15.96.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.
@@ -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,10 +143,11 @@ 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>;
148
- getValidationFunction(): ValidationFunction<OUT, AjvValidationError>;
150
+ getValidationFunction(opt?: AjvValidationOptions): ValidationFunction<OUT, AjvValidationError>;
149
151
  /**
150
152
  * Specify a function to be called after the normal validation is finished.
151
153
  *
@@ -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)
@@ -310,12 +330,13 @@ export class JSchema {
310
330
  const inputName = this._cfg?.inputName || (builtSchema.$id ? _substringBefore(builtSchema.$id, '.') : undefined);
311
331
  return executeValidation(fn, builtSchema, input, opt, inputName);
312
332
  }
313
- getValidationFunction() {
314
- return (input, opt) => {
333
+ getValidationFunction(opt = {}) {
334
+ return (input, opt2) => {
315
335
  return this.getValidationResult(input, {
316
- mutateInput: opt?.mutateInput,
317
- inputName: opt?.inputName,
318
- inputId: opt?.inputId,
336
+ ajv: opt.ajv,
337
+ mutateInput: opt2?.mutateInput ?? opt.mutateInput,
338
+ inputName: opt2?.inputName ?? opt.inputName,
339
+ inputId: opt2?.inputId ?? opt.inputId,
319
340
  });
320
341
  };
321
342
  }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.94.0",
4
+ "version": "15.96.0",
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",
@@ -19,7 +20,7 @@
19
20
  },
20
21
  "devDependencies": {
21
22
  "@typescript/native-preview": "7.0.0-dev.20260201.1",
22
- "@naturalcycles/dev-lib": "20.36.0"
23
+ "@naturalcycles/dev-lib": "18.4.2"
23
24
  },
24
25
  "exports": {
25
26
  ".": "./dist/index.js",
@@ -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
@@ -447,12 +472,15 @@ export class JSchema<OUT, Opt> {
447
472
  return executeValidation<OUT>(fn, builtSchema, input, opt, inputName)
448
473
  }
449
474
 
450
- getValidationFunction(): ValidationFunction<OUT, AjvValidationError> {
451
- return (input, opt) => {
475
+ getValidationFunction(
476
+ opt: AjvValidationOptions = {},
477
+ ): ValidationFunction<OUT, AjvValidationError> {
478
+ return (input, opt2) => {
452
479
  return this.getValidationResult(input, {
453
- mutateInput: opt?.mutateInput,
454
- inputName: opt?.inputName,
455
- inputId: opt?.inputId,
480
+ ajv: opt.ajv,
481
+ mutateInput: opt2?.mutateInput ?? opt.mutateInput,
482
+ inputName: opt2?.inputName ?? opt.inputName,
483
+ inputId: opt2?.inputId ?? opt.inputId,
456
484
  })
457
485
  }
458
486
  }