Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(concerto): fix bugs when id field has regex #630

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion packages/concerto-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const c = require('ansi-colors');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const RandExp = require('randexp');

const Logger = require('@accordproject/concerto-util').Logger;
const FileWriter = require('@accordproject/concerto-util').FileWriter;
Expand Down Expand Up @@ -317,10 +318,23 @@ class Commands {
};

const classDeclaration = modelManager.getType(concept);

let idFieldName = classDeclaration.getIdentifierFieldName();
let idField = classDeclaration.getProperty(idFieldName);
let id = 'resource1';
if (idField) {
if(idField.isTypeScalar && idField.isTypeScalar()){
idField = idField.getScalarField();
}
if(idField.validator && idField.validator.regex) {
id = new RandExp(idField.validator.regex.source, idField.validator.regex.flags).gen();
}
}

const resource = factory.newResource(
classDeclaration.getNamespace(),
classDeclaration.getName(),
classDeclaration.isIdentified() ? 'resource1' : null,
classDeclaration.isIdentified() ? id : null,
factoryOptions
);
const serializer = new Serializer(factory, modelManager);
Expand Down
143 changes: 55 additions & 88 deletions packages/concerto-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/concerto-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@accordproject/concerto-util": "3.6.0",
"ansi-colors": "4.1.3",
"glob": "^7.2.0",
"randexp": "^0.5.3",
"semver": "7.3.5",
"yargs": "17.3.1"
},
Expand Down
22 changes: 21 additions & 1 deletion packages/concerto-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { Parser } = require('@accordproject/concerto-cto');

describe('concerto-cli', () => {
const models = [path.resolve(__dirname, 'models/dom.cto'),path.resolve(__dirname, 'models/money.cto')];
const offlineModels = [path.resolve(__dirname, 'models/contract.cto'),path.resolve(__dirname, 'models/dom.cto'),path.resolve(__dirname, 'models/money.cto')];
const offlineModels = [path.resolve(__dirname, 'models/contract.cto'),path.resolve(__dirname, 'models/dom.cto'),path.resolve(__dirname, 'models/money.cto'),path.resolve(__dirname, 'models/person.cto')];
const input1 = path.resolve(__dirname, 'data/input1.json');
const input2 = path.resolve(__dirname, 'data/input2.json');
const inputText1 = fs.readFileSync(input1, 'utf8');
Expand Down Expand Up @@ -641,5 +641,25 @@ describe('concerto-cli', () => {
obj.$class.should.equal('org.accordproject.cicero.dom.ContractTemplate');
Object.keys(obj).should.eql(['$class', 'metadata', 'content', 'id', '$identifier']);
});

it('should generate an identified object with an identifier property with regex', async () => {
const obj = await Commands.generate(
offlineModels,
'[email protected]',
'sample',
{ offline: true, optionalFields: true }
);
obj.ssn.should.match(/\d{3}-\d{2}-\d{4}/);
});

it('should generate an identified object identified by a scalar', async () => {
const obj = await Commands.generate(
offlineModels,
'[email protected]',
'sample',
{ offline: true, optionalFields: true }
);
(typeof obj.ssn).should.equal('string');
});
});
});
19 changes: 19 additions & 0 deletions packages/concerto-cli/test/models/person.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace [email protected]

concept Person identified by ssn {
o String name
@description("Height (cm)")
o Double height range=[0.0,]
o DateTime dateOfBirth
o String ssn regex=/\d{3}-\d{2}-\d{4}/
}

scalar SSN extends String regex=/\d{3}-\d{2}-\d{4}/

concept Person2 identified by ssn {
o String name
@description("Height (cm)")
o Double height range=[0.0,]
o DateTime dateOfBirth
o SSN ssn
}
11 changes: 11 additions & 0 deletions packages/concerto-core/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ class Factory {
type: type
}));
}

if (id) {
let idFullField = classDecl.getProperty(idField);
if (idFullField?.isTypeScalar?.()){
idFullField = idFullField.getScalarField();
}
// if regex on identifier field & provided id does not match regex, throw error
if(idFullField?.validator?.regex && (idFullField.validator?.regex.test(id) === false)) {
throw new Error('Provided id does not match regex: ' + idFullField?.validator?.regex);
}
}
} else if(id) {
throw new Error('Type is not identifiable ' + classDecl.getFullyQualifiedName());
}
Expand Down
Loading