Skip to content

Commit

Permalink
feat: load schema from {name}.schema.json files (#13)
Browse files Browse the repository at this point in the history
* feat: add schema to endpoints

* refactor: getSchema

* feat(getSchema): returning schema if it exists

* fix: createSchema implementation in generatePath

* feat: use ajv

* feat(uuuuuh): Working schema validation

* chore: Testing create issuer contracts

* feat: Add fetching result from endpoint

* style: whitespace

* fix: improved ajv import

* fix: removed unused file

* fix: removed unused extension

* feat: add json schema validation on load

* refactor: keys can now be undefined

---------

Co-authored-by: Puria Nafisi Azizi <[email protected]>
  • Loading branch information
bbtgnn and puria authored Nov 17, 2023
1 parent c3ec64f commit 8796b91
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"debug": "nodemon --inspect -e ts -w ./src -x pnpm run watch",
"test": "jest"
},
"engines": {
"node": ">=20.0.0 <22.0.0"
},
"bin": {
"ncr": "dist/index.cjs"
},
Expand Down
29 changes: 21 additions & 8 deletions src/directory.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import LiveDirectory from 'live-directory';
import { config } from './cli.js';
import { Endpoints } from './types.js';
import { validateJSONSchema } from './utils.js';

export class Directory {
private static instance: Directory;
private liveDirectory: LiveDirectory;

private constructor() {
this.liveDirectory = new LiveDirectory(config.zencodeDirectory, {
static: false,
filter: {
keep: {
extensions: ['zen', 'schema', 'conf', 'json']
extensions: ['zen', 'conf', 'json']
},
ignore: {
// extensions: ['keys']
}
}
});
}

private getContent(name: string) {
return this.liveDirectory.get(name)?.content.toString('utf-8');
}
Expand All @@ -26,7 +29,6 @@ export class Directory {
if (!Directory.instance) {
Directory.instance = new Directory();
}

return Directory.instance;
}

Expand All @@ -44,21 +46,32 @@ export class Directory {
contract:
`Rule unknown ignore\nRule check version ${config.zenroomVersion}\n` +
Buffer.from(c.content).toString('utf-8'),
keys: this.getKeys(path),
keys: this.getJSON(path, 'keys'),
conf: this.getContent(path + '.conf') || '',
schema: this.getJSON(path, 'schema')
schema: this.getJSONSchema(path)
});
}
});
return result;
}

private getKeys(path: string) {
private getJSON(path: string, type: 'schema' | 'keys') {
try {
const k = this.getContent(path + '.keys.json');
if (k) return JSON.parse(k);
const k = this.getContent(`${path}.${type}.json`);
if (!k) return undefined;
else return JSON.parse(k);
} catch (_e) {
throw new Error(`${path}.keys malformed JSON`);
throw new Error(`${path}.${type}.json: malformed JSON`);
}
}

private getJSONSchema(path: string) {
try {
const schema = this.getJSON(path, 'schema');
validateJSONSchema(schema);
return schema;
} catch (e) {
throw e;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Config {
export interface Endpoints {
path: string;
contract: string;
keys: JSON;
keys?: JSON | undefined;
conf: string;
schema?: JSON | undefined;
}
Expand Down
15 changes: 13 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { introspect } from 'zenroom';
import { Type } from '@sinclair/typebox';
import { TypeCompiler, ValueErrorIterator } from '@sinclair/typebox/compiler';
import { ValueErrorIterator } from '@sinclair/typebox/compiler';
import { Codec, Endpoints } from './types';
import _ from 'lodash';
import Ajv from 'ajv';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const getSchema = async (endpoints: Endpoints) => {
};

export const validateData = (schema: any, data: JSON | Record<string, unknown>) => {
const ajv = new Ajv.default();
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) throw new Error(JSON.stringify(validate.errors));
Expand Down Expand Up @@ -60,3 +60,14 @@ export const handleArrayBuffer = (message: ArrayBuffer | string) => {
}
return JSON.parse(message);
};

//

export function validateJSONSchema(schema: JSON): void {
try {
const ajv = new Ajv();
ajv.compile(schema);
} catch (e) {
throw e;
}
}

0 comments on commit 8796b91

Please sign in to comment.