Skip to content

Commit aa8c01e

Browse files
committed
WIP
1 parent 21f0c29 commit aa8c01e

File tree

7 files changed

+27255
-21
lines changed

7 files changed

+27255
-21
lines changed

examples/ajv.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Simple validation
2+
let db = require('../test/fixtures/db.json');
3+
let schemas = require('../test/fixtures/schemas.js');
4+
const Ajv = require('ajv');
5+
6+
let ajv = new Ajv({
7+
schemas: schemas,
8+
allErrors: true,
9+
verbose: true
10+
});
11+
12+
let validate = ajv.getSchema(`db`);
13+
14+
if (!validate(db)) {
15+
console.error(validate.errors);
16+
}

examples/ajv0.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Simple validation
2+
let db = require('../test/fixtures/db.json');
3+
let schemas = require('../test/fixtures/schemas.js');
4+
const Ajv = require('ajv');
5+
6+
let ajv = new Ajv({
7+
schemas: schemas,
8+
allErrors: true,
9+
verbose: true
10+
});
11+
12+
let validate = ajv.getSchema(`category`);
13+
14+
if (!validate({
15+
id: 'test'
16+
})) {
17+
console.error(validate.errors);
18+
}

db.js renamed to index.js

+8-21
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ const path = require('path');
22
const fs = require('fs');
33
const Ajv = require('ajv');
44
const pluralize = require('pluralize');
5-
const transliterate = require('../../../libs/transliterate');
5+
const transliterate = require('@javascriptru/transliterate');
66
const uuid = require('uuid/v4');
77

8-
const dbPath = path.join(__dirname, '../data/db.json');
9-
const schemaPath = path.join(__dirname, '../db.schemas.js');
8+
module.exports = class Db {
109

11-
class Db {
12-
13-
constructor(filePath, schemasPath) {
14-
this.filePath = filePath;
10+
constructor({dataPath, schemasPath}) {
11+
this.filePath = dataPath;
1512
let schemas = require(schemasPath);
1613
this.ajv = new Ajv({
1714
schemas,
@@ -20,14 +17,6 @@ class Db {
2017
});
2118
}
2219

23-
static instance() {
24-
if (!this._instance) {
25-
this._instance = new Db(dbPath, schemaPath);
26-
this._instance.load();
27-
}
28-
return this._instance;
29-
}
30-
3120
getAll() {
3221
return this.data;
3322
}
@@ -81,11 +70,12 @@ class Db {
8170

8271
// product/db/...
8372
getValidate(name) {
84-
return this.ajv.getSchema(`https://javascript.info/schemas/${name}.json`);
73+
return this.ajv.getSchema(name);
8574
}
8675

87-
validateSelf() {
88-
let validate = this.ajv.getSchema(`https://javascript.info/schemas/db.json`);
76+
validateAll() {
77+
// fixme
78+
let validate = this.ajv.getSchema('db');
8979
if (!validate({db: this.data})) {
9080
console.error(validate.errors);
9181
throw new Error("Validation error");
@@ -126,6 +116,3 @@ class Db {
126116
}
127117

128118
}
129-
130-
131-
module.exports = Db.instance();

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"ajv": "^6.12.0",
2222
"pluralize": "^8.0.0",
2323
"uuid": "^7.0.1"
24+
},
25+
"devDependencies": {
26+
"mocha": "^7.1.0"
2427
}
2528
}

test/db.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const Db = require('../');
2+
const path = require('path');
3+
4+
describe("Db", function() {
5+
it("validates all", function() {
6+
const db = new Db({
7+
dataPath: path.resolve(__dirname, 'fixtures/db.json'),
8+
schemasPath: path.resolve(__dirname, 'fixtures/schemas.js')
9+
});
10+
11+
db.validateAll();
12+
});
13+
});

0 commit comments

Comments
 (0)