1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const ajv_1 = __importDefault(require("ajv"));
|
7 | const ajv_keywords_1 = __importDefault(require("ajv-keywords"));
|
8 | const schemas_1 = require("../transactions/types/schemas");
|
9 | const formats_1 = require("./formats");
|
10 | const keywords_1 = require("./keywords");
|
11 | const schemas_2 = require("./schemas");
|
12 | class Validator {
|
13 | constructor(options) {
|
14 | this.transactionSchemas = new Set();
|
15 | const ajv = new ajv_1.default({
|
16 | ...{
|
17 | $data: true,
|
18 | schemas: schemas_2.schemas,
|
19 | removeAdditional: true,
|
20 | extendRefs: true,
|
21 | },
|
22 | ...options,
|
23 | });
|
24 | ajv_keywords_1.default(ajv);
|
25 | for (const addKeyword of keywords_1.keywords) {
|
26 | addKeyword(ajv);
|
27 | }
|
28 | for (const addFormat of formats_1.formats) {
|
29 | addFormat(ajv);
|
30 | }
|
31 | this.ajv = ajv;
|
32 | }
|
33 | static make(options = {}) {
|
34 | return new Validator(options);
|
35 | }
|
36 | getInstance() {
|
37 | return this.ajv;
|
38 | }
|
39 | validate(schemaKeyRef, data) {
|
40 | try {
|
41 | this.ajv.validate(schemaKeyRef, data);
|
42 | const error = this.ajv.errors ? this.ajv.errorsText() : undefined;
|
43 | return { value: data, error, errors: this.ajv.errors };
|
44 | }
|
45 | catch (error) {
|
46 | return { value: undefined, error: error.stack, errors: [] };
|
47 | }
|
48 | }
|
49 | addFormat(name, format) {
|
50 | this.ajv.addFormat(name, format);
|
51 | }
|
52 | addKeyword(keyword, definition) {
|
53 | this.ajv.addKeyword(keyword, definition);
|
54 | }
|
55 | addSchema(schema, key) {
|
56 | this.ajv.addSchema(schema, key);
|
57 | }
|
58 | removeKeyword(keyword) {
|
59 | this.ajv.removeKeyword(keyword);
|
60 | }
|
61 | removeSchema(schemaKeyRef) {
|
62 | this.ajv.removeSchema(schemaKeyRef);
|
63 | }
|
64 | extendTransaction(schema, remove) {
|
65 | if (remove) {
|
66 | this.transactionSchemas.delete(schema.$id);
|
67 | this.ajv.removeSchema(schema.$id);
|
68 | this.ajv.removeSchema(`${schema.$id}Signed`);
|
69 | this.ajv.removeSchema(`${schema.$id}Strict`);
|
70 | }
|
71 | else {
|
72 | this.transactionSchemas.add(schema.$id);
|
73 | this.ajv.addSchema(schema);
|
74 | this.ajv.addSchema(schemas_1.signedSchema(schema));
|
75 | this.ajv.addSchema(schemas_1.strictSchema(schema));
|
76 | }
|
77 | this.updateTransactionArray();
|
78 | }
|
79 | updateTransactionArray() {
|
80 | this.ajv.removeSchema("block");
|
81 | this.ajv.removeSchema("transactions");
|
82 | this.ajv.addSchema({
|
83 | $id: "transactions",
|
84 | type: "array",
|
85 | additionalItems: false,
|
86 | items: { oneOf: [...this.transactionSchemas].map(schema => ({ $ref: `${schema}Signed` })) },
|
87 | });
|
88 | this.ajv.addSchema(schemas_2.schemas.block);
|
89 | }
|
90 | }
|
91 | exports.Validator = Validator;
|
92 | exports.validator = Validator.make();
|
93 |
|
\ | No newline at end of file |