Skip to content

Commit

Permalink
feat: added exception for reserved keywords (#100)
Browse files Browse the repository at this point in the history
* feat: added exception for reserved keywords

Signed-off-by: Kailash Kejriwal <[email protected]>

* chore: added tests

Signed-off-by: Kailash Kejriwal <[email protected]>

* feat: added ModelUtil method

Signed-off-by: Kailash Kejriwal <[email protected]>

* Update lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js

Co-authored-by: jonathan-casey <[email protected]>
Signed-off-by: Dan Selman <[email protected]>

* Update lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js

Co-authored-by: jonathan-casey <[email protected]>
Signed-off-by: Dan Selman <[email protected]>

---------

Signed-off-by: Kailash Kejriwal <[email protected]>
Signed-off-by: Dan Selman <[email protected]>
Co-authored-by: Dan Selman <[email protected]>
Co-authored-by: jonathan-casey <[email protected]>
  • Loading branch information
3 people authored May 7, 2024
1 parent a2463e0 commit 5204904
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const draft7MetaSchema = require('ajv/dist/refs/json-schema-draft-07.json');
const addFormats = require('ajv-formats');
const getValue = require('get-value');
const { Identifiers } = require('@accordproject/concerto-util');
const { ModelUtil } = require('@accordproject/concerto-core');

const {
LocalReference,
Expand Down Expand Up @@ -614,7 +615,10 @@ class JsonSchemaVisitor {
};

// Handle reserved properties.
if (['$identifier', '$class', '$timestamp'].includes(propertyName)) {
if (ModelUtil.isPrivateSystemProperty(propertyName)) {
throw new Error(`${propertyName} is a reserved keyword and cannot be used as a property`);
}
if (ModelUtil.isSystemProperty(propertyName)) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions test/codegen/fromJsonSchema/cto/jsonSchemaVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ describe('JsonSchemaVisitor', () => {
);
}).should.throw('schema is invalid: data/properties/Foo/type must be equal to one of the allowed values, data/properties/Foo/type must be array, data/properties/Foo/type must match a schema in anyOf');
});
it('should not generate when reserved keywords are used', async () => {
(function () {
const jsonSchemaModelClass = new JsonSchemaModel({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
'$namespace': { type: 'integer' }
}
});

jsonSchemaModelClass.accept(
jsonSchemaVisitor, jsonSchemaVisitorParameters
);
}).should.throw('$namespace is a reserved keyword and cannot be used as a property');
});

it('should generate for a simple definition', async () => {
const inferredConcertoJsonModel = JsonSchemaVisitor
Expand Down

0 comments on commit 5204904

Please sign in to comment.