Skip to content

Commit

Permalink
experiment with json-schema-to-X
Browse files Browse the repository at this point in the history
  • Loading branch information
bdittes committed Nov 5, 2019
1 parent c93b7c5 commit 05a0453
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 72 deletions.
13 changes: 0 additions & 13 deletions app/api/thesauris/dictionariesModel.js

This file was deleted.

21 changes: 21 additions & 0 deletions app/api/thesauris/dictionariesModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @format */
import { instanceModel } from 'api/odm';
import createMongooseSchema from 'json-schema-to-mongoose';
import mongoose from 'mongoose';

import schema from './dictionariesSchema';
import { Thesaurus } from './dictionariesType';

// const dictionarySchema = new mongoose.Schema({
// name: String,
// values: [
// {
// id: String,
// label: { type: String },
// values: mongoose.Schema.Types.Mixed,
// },
// ],
// });

export interface ThesaurusDocument extends mongoose.Document, Thesaurus {}
export default instanceModel<ThesaurusDocument>('dictionaries', createMongooseSchema({}, schema));
32 changes: 32 additions & 0 deletions app/api/thesauris/dictionariesSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @format */

export default {
$async: true,
title: 'Thesaurus',
type: 'object',
required: ['name'],
properties: {
name: {
type: 'string',
uniqueName: '',
minLength: 1,
},
values: {
type: 'array',
items: {
type: 'object',
required: ['label'],
properties: {
label: {
type: 'id',
minLength: 1,
},
label: {
type: 'string',
minLength: 1,
},
},
},
},
},
};
15 changes: 15 additions & 0 deletions app/api/thesauris/dictionariesType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* tslint:disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface Thesaurus {
name: string;
values?: {
label: string;
[k: string]: any;
}[];
[k: string]: any;
}
53 changes: 0 additions & 53 deletions app/api/thesauris/validateThesauri.js

This file was deleted.

34 changes: 34 additions & 0 deletions app/api/thesauris/validateThesauri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** @format */

/* eslint-disable max-params */

import Ajv from 'ajv';
import ajvKeywords from 'ajv-keywords';
import model from './dictionariesModel';
import schema from './dictionariesSchema';
import { Thesaurus } from './dictionariesType';

const ajv = ajvKeywords(Ajv({ allErrors: true }), ['uniqueItemProperties']);

ajv.addKeyword('uniqueName', {
async: true,
validate: async (
_config: any,
_value: any,
_propertySchema: any,
_property: any,
thesauri: Thesaurus
) => {
const [duplicated] = await model.get({
_id: { $ne: thesauri._id },
name: new RegExp(`^${thesauri.name}$` || '', 'i'),
});

if (duplicated) {
return false;
}
return true;
},
});

export const validateThesauri = ajv.compile(schema);
18 changes: 18 additions & 0 deletions build_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @format */

/* eslint-disable import/first */
require('dotenv').config();
require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] });

const fs = require('fs');
const { compile } = require('json-schema-to-typescript');

const buildSchema = path => {
const schema = require(`./${path}Schema`).default;

compile(schema, schema.title).then(ts => {
fs.writeFileSync(`./${path}Type.d.ts`, ts);
});
};

buildSchema('app/api/thesauris/dictionaries');
1 change: 1 addition & 0 deletions external_modules/ajv-keywords.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'ajv-keywords';
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
"joi": "^13.6.0",
"joi-objectid": "^2.0.0",
"json-loader": "^0.5.4",
"json-schema-to-mongoose": "^0.2.2",
"json-schema-to-typescript": "^7.1.0",
"jvent": "1.0.2",
"macaddress": "0.2.9",
"mark.js": "^8.11.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"UI/*": ["./app/react/UI/*"]
}
},
"include": ["app"],
"include": ["app", "external_modules"],
"exclude": ["node_modules", "**/node_modules/*", "fixtures", "dist", "prod"]
}
Loading

0 comments on commit 05a0453

Please sign in to comment.