Skip to content

Commit e659537

Browse files
committed
validation: add ajv formats to where needed
1 parent c6dbebc commit e659537

4 files changed

Lines changed: 11 additions & 27 deletions

File tree

packages/binding-opcua/src/codec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { DataSchema } from "@node-wot/td-tools";
1818
import { DataValue } from "node-opcua-data-value";
1919
import { DataType, Variant } from "node-opcua-variant";
2020
import Ajv from "ajv";
21-
import "ajv-formats"; /// to get date again !
21+
import addFormats from "ajv-formats";
2222

2323
// see https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
2424
import {
@@ -36,7 +36,7 @@ const { debug } = createLoggers("binding-opcua", "codec");
3636
// Strict mode has a lot of other checks and it prevents runtime unexpected problems
3737
// TODO: in the future we should use the strict mode
3838
const ajv = new Ajv({ strict: false });
39-
39+
addFormats(ajv);
4040
/**
4141
* this schema, describe the node-opcua JSON format for a DataValue object
4242
*

packages/core/src/helpers.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import * as TD from "@node-wot/td-tools";
3333
import * as TDT from "wot-thing-description-types";
3434
import { ContentSerdes } from "./content-serdes";
3535
import Ajv, { ValidateFunction, ErrorObject } from "ajv";
36+
import addFormats from "ajv-formats";
3637
import TDSchema from "wot-thing-description-types/schema/td-json-schema-validation.json";
3738
import { DataSchemaValue, ExposedThingInit } from "wot-typescript-definitions";
3839
import { SomeJSONSchema } from "ajv/dist/types/json-schema";
@@ -45,16 +46,9 @@ const { debug, error, warn } = createLoggers("core", "helpers");
4546

4647
const tdSchema = TDSchema;
4748
// RegExps take from https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts
48-
const ajv = new Ajv({ strict: false })
49-
.addFormat(
50-
"iri-reference",
51-
/^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a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9+\-.]*:)(?:\/?\/)?[^\s]*$/)
54-
.addFormat(
55-
"date-time",
56-
/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/
57-
);
49+
const ajv = new Ajv({ strict: false });
50+
addFormats(ajv);
51+
5852
export default class Helpers implements Resolver {
5953
static tsSchemaValidator = ajv.compile(Helpers.createExposeThingInitSchema(tdSchema)) as ValidateFunction;
6054

packages/core/src/interaction-output.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ProtocolHelpers } from "./core";
1919
import { DataSchemaError, NotReadableError, NotSupportedError } from "./errors";
2020
import { Content } from "./content";
2121
import Ajv from "ajv";
22+
import addFormats from "ajv-formats";
2223
import { createLoggers } from "./logger";
2324

2425
const { debug, warn } = createLoggers("core", "interaction-output");
@@ -30,6 +31,7 @@ const { debug, warn } = createLoggers("core", "interaction-output");
3031
// Strict mode has a lot of other checks and it prevents runtime unexpected problems
3132
// TODO: in the future we should use the strict mode
3233
const ajv = new Ajv({ strict: false });
34+
addFormats(ajv);
3335

3436
export class InteractionOutput implements WoT.InteractionOutput {
3537
#content: Content;

packages/td-tools/src/thing-model-helpers.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
********************************************************************************/
1515

1616
import Ajv, { ValidateFunction, ErrorObject } from "ajv";
17+
import addFormats from "ajv-formats";
1718
import * as http from "http";
1819
import * as https from "https";
1920
import * as fs from "fs";
@@ -32,21 +33,8 @@ const logError = debug(`${namespace}:error`);
3233

3334
const tmSchema = TMSchema;
3435
// RegExps take from https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts
35-
const ajv = new Ajv({ strict: false })
36-
.addFormat(
37-
"iri-reference",
38-
/^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~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a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i
43-
) // TODO: check me
44-
.addFormat("uri", /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/)
45-
.addFormat("json-pointer", /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/) // TODO: check me
46-
.addFormat(
47-
"date-time",
48-
/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/
49-
);
36+
const ajv = new Ajv({ strict: false });
37+
addFormats(ajv);
5038

5139
export type LINK_TYPE = "tm:extends" | "tm:submodel";
5240
export type AFFORDANCE_TYPE = "properties" | "actions" | "events";

0 commit comments

Comments
 (0)