diff --git a/package-lock.json b/package-lock.json index 3ec51f5564..5ae24aa492 100644 --- a/package-lock.json +++ b/package-lock.json @@ -60,7 +60,7 @@ "typescript": "4.6.3", "urijs": "1.19.11", "uuid": "8.3.2", - "webpack": "5.75.0", + "webpack": "5.76.0", "webpack-cli": "4.9.1", "xregexp": "5.1.1", "yaml": "2.0.0-9", @@ -18793,9 +18793,9 @@ "license": "BSD-2-Clause" }, "node_modules/webpack": { - "version": "5.75.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.75.0.tgz", - "integrity": "sha512-piaIaoVJlqMsPtX/+3KTTO6jfvrSYgauFVdt8cr9LTHKmcq/AMd4mhzsiP7ZF/PGRNPGA8336jldh9l2Kt2ogQ==", + "version": "5.76.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.76.0.tgz", + "integrity": "sha512-l5sOdYBDunyf72HW8dF23rFtWq/7Zgvt/9ftMof71E/yUb1YLOBmTgA2K4vQthB3kotMrSj609txVE0dnr2fjA==", "dependencies": { "@types/eslint-scope": "^3.7.3", "@types/estree": "^0.0.51", diff --git a/packages/concerto-cli/lib/commands.js b/packages/concerto-cli/lib/commands.js index ae4968688e..20e0a7098d 100644 --- a/packages/concerto-cli/lib/commands.js +++ b/packages/concerto-cli/lib/commands.js @@ -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; @@ -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); diff --git a/packages/concerto-cli/package-lock.json b/packages/concerto-cli/package-lock.json index 4adc81cc1e..89417d02d2 100644 --- a/packages/concerto-cli/package-lock.json +++ b/packages/concerto-cli/package-lock.json @@ -15,10 +15,9 @@ "@accordproject/concerto-metamodel": "3.6.0", "@accordproject/concerto-tools": "3.6.0", "@accordproject/concerto-util": "3.6.0", - "@openapi-contrib/openapi-schema-to-json-schema": "^3.2.0", "ansi-colors": "4.1.3", "glob": "^7.2.0", - "json-schema-migrate": "^2.0.0", + "randexp": "^0.5.3", "semver": "7.3.5", "yargs": "17.3.1" }, @@ -44,11 +43,11 @@ } }, "node_modules/@accordproject/concerto-analysis": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-analysis/-/concerto-analysis-3.2.0.tgz", - "integrity": "sha512-is73ZDgFC49ap6ZeL0JQAL8t2VrPeCqrhvXsvOwmK+8WI3WyDfrbk4zu3BnmmztWXeuPfcWwkovA3AtLRET5gA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-analysis/-/concerto-analysis-3.6.0.tgz", + "integrity": "sha512-0Ucj3Yvp0dknHJzP9qjk8H0+l4/bV9qzMNsGVBK2OOrpK2EtsA3Vd9VYHrrZXVv89/utPsLa48s0jYA5gtXEBA==", "dependencies": { - "@accordproject/concerto-core": "3.2.0", + "@accordproject/concerto-core": "3.6.0", "semver": "7.3.5" }, "engines": { @@ -57,13 +56,13 @@ } }, "node_modules/@accordproject/concerto-core": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-core/-/concerto-core-3.2.0.tgz", - "integrity": "sha512-rBxtsacY1fAKdplYvNf2bRdwqmsfoGOujEHBbpks5gZQP+kXxGmnndzXU+Zkjsxbs/AQKsD5LG5JeOZuYGFD2A==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-core/-/concerto-core-3.6.0.tgz", + "integrity": "sha512-4ljdSC2R0VBtrvTFJ8AEA9hv4YOKIZTKtLcgdcijFKs1jIRPJjPv5H4D8lwlY0nb4Og7DPXexqG3iacbuIhh8A==", "dependencies": { - "@accordproject/concerto-cto": "3.2.0", - "@accordproject/concerto-metamodel": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-cto": "3.6.0", + "@accordproject/concerto-metamodel": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "dayjs": "1.10.8", "debug": "4.3.1", "lorem-ipsum": "2.0.3", @@ -79,12 +78,12 @@ } }, "node_modules/@accordproject/concerto-cto": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-cto/-/concerto-cto-3.2.0.tgz", - "integrity": "sha512-rCEtJrG9njmMZXt1t+VCb7gWf9aj33EhS5eWU7bYUtS0PfwxtIxuP44BJieohdFAZ4k4patLOWDBFHy6xRwNlA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-cto/-/concerto-cto-3.6.0.tgz", + "integrity": "sha512-4b27wVKq2RxfPG6mRZ4VmWd9iwAM4QRXZRvfHCN57e6GiuosXDwhbZc1rx789eKukSTeOv2LJXuwCFFEk6qamQ==", "dependencies": { - "@accordproject/concerto-metamodel": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-metamodel": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "path-browserify": "1.0.1" }, "engines": { @@ -93,11 +92,11 @@ } }, "node_modules/@accordproject/concerto-metamodel": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-metamodel/-/concerto-metamodel-3.2.0.tgz", - "integrity": "sha512-tLOCVCwAlrIE9S5w3ycH0OpjDdjImT0lu3hmuW/4al9U9ZtKT7baGCRSrDl2jdiY7om46OOZdIFdN92tO0gNPA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-metamodel/-/concerto-metamodel-3.6.0.tgz", + "integrity": "sha512-5ojIrvq/hBAxwXH2Vwp5acVVgt11S76vQF3aWrOp4nN+PU8+H/kFYR26y19Pi3Ndwd5VWGRXzRVntEuNIrZrhQ==", "dependencies": { - "@accordproject/concerto-util": "3.2.0" + "@accordproject/concerto-util": "3.6.0" }, "engines": { "node": ">=14", @@ -105,12 +104,12 @@ } }, "node_modules/@accordproject/concerto-tools": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-tools/-/concerto-tools-3.2.0.tgz", - "integrity": "sha512-oxoPQoIL8PmIvgmiDUUXN3Ly0iUPpx30Oj3vSlO/Qaks/7Ywe/Y8yxCQk40kZWecQKyCQgk0K5fDNHG4ctaWlg==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-tools/-/concerto-tools-3.6.0.tgz", + "integrity": "sha512-pbN3m+9TMUJAJKbuWIaVfypIscMkc16hWtPZrMgiWslYTC75NA3HIu/Uk0JPL+sBq3/xZ8t7yKS3llcIaRBbrg==", "dependencies": { - "@accordproject/concerto-core": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-core": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "ajv": "8.10.0", "ajv-formats": "2.1.1", "camelcase": "6.3.0", @@ -123,9 +122,9 @@ } }, "node_modules/@accordproject/concerto-util": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-util/-/concerto-util-3.2.0.tgz", - "integrity": "sha512-evzBWY+BLPSVgB1OgGKhT2cIXz6kZAhPqG0yWjaHAyUJYpvyN0PmBzmc4bI5AiL5RUseyBVkArfl2IAE9jz49w==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-util/-/concerto-util-3.6.0.tgz", + "integrity": "sha512-HLJuXoQkVmOfvr7nZ/0cDkSnJCL2nQeQ21tdDIpg2jJohIbzPzBSHTCltuUU5TBEK2tkLLjNXymeQD6PNFmSSg==", "dependencies": { "@supercharge/promise-pool": "1.7.0", "axios": "0.23.0", @@ -807,14 +806,6 @@ "@jridgewell/sourcemap-codec": "1.4.14" } }, - "node_modules/@openapi-contrib/openapi-schema-to-json-schema": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@openapi-contrib/openapi-schema-to-json-schema/-/openapi-schema-to-json-schema-3.2.0.tgz", - "integrity": "sha512-Gj6C0JwCr8arj0sYuslWXUBSP/KnUlEGnPW4qxlXvAl543oaNQgMgIgkQUA6vs5BCCvwTEiL8m/wdWzfl4UvSw==", - "dependencies": { - "fast-deep-equal": "^3.1.3" - } - }, "node_modules/@sinonjs/commons": { "version": "1.8.5", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.5.tgz", @@ -2494,14 +2485,6 @@ "node": ">=4" } }, - "node_modules/json-schema-migrate": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/json-schema-migrate/-/json-schema-migrate-2.0.0.tgz", - "integrity": "sha512-r38SVTtojDRp4eD6WsCqiE0eNDt4v1WalBXb9cyZYw9ai5cGtBwzRNWjHzJl38w6TxFkXAIA7h+fyX3tnrAFhQ==", - "dependencies": { - "ajv": "^8.0.0" - } - }, "node_modules/json-schema-traverse": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", @@ -4198,22 +4181,22 @@ }, "dependencies": { "@accordproject/concerto-analysis": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-analysis/-/concerto-analysis-3.2.0.tgz", - "integrity": "sha512-is73ZDgFC49ap6ZeL0JQAL8t2VrPeCqrhvXsvOwmK+8WI3WyDfrbk4zu3BnmmztWXeuPfcWwkovA3AtLRET5gA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-analysis/-/concerto-analysis-3.6.0.tgz", + "integrity": "sha512-0Ucj3Yvp0dknHJzP9qjk8H0+l4/bV9qzMNsGVBK2OOrpK2EtsA3Vd9VYHrrZXVv89/utPsLa48s0jYA5gtXEBA==", "requires": { - "@accordproject/concerto-core": "3.2.0", + "@accordproject/concerto-core": "3.6.0", "semver": "7.3.5" } }, "@accordproject/concerto-core": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-core/-/concerto-core-3.2.0.tgz", - "integrity": "sha512-rBxtsacY1fAKdplYvNf2bRdwqmsfoGOujEHBbpks5gZQP+kXxGmnndzXU+Zkjsxbs/AQKsD5LG5JeOZuYGFD2A==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-core/-/concerto-core-3.6.0.tgz", + "integrity": "sha512-4ljdSC2R0VBtrvTFJ8AEA9hv4YOKIZTKtLcgdcijFKs1jIRPJjPv5H4D8lwlY0nb4Og7DPXexqG3iacbuIhh8A==", "requires": { - "@accordproject/concerto-cto": "3.2.0", - "@accordproject/concerto-metamodel": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-cto": "3.6.0", + "@accordproject/concerto-metamodel": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "dayjs": "1.10.8", "debug": "4.3.1", "lorem-ipsum": "2.0.3", @@ -4225,30 +4208,30 @@ } }, "@accordproject/concerto-cto": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-cto/-/concerto-cto-3.2.0.tgz", - "integrity": "sha512-rCEtJrG9njmMZXt1t+VCb7gWf9aj33EhS5eWU7bYUtS0PfwxtIxuP44BJieohdFAZ4k4patLOWDBFHy6xRwNlA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-cto/-/concerto-cto-3.6.0.tgz", + "integrity": "sha512-4b27wVKq2RxfPG6mRZ4VmWd9iwAM4QRXZRvfHCN57e6GiuosXDwhbZc1rx789eKukSTeOv2LJXuwCFFEk6qamQ==", "requires": { - "@accordproject/concerto-metamodel": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-metamodel": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "path-browserify": "1.0.1" } }, "@accordproject/concerto-metamodel": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-metamodel/-/concerto-metamodel-3.2.0.tgz", - "integrity": "sha512-tLOCVCwAlrIE9S5w3ycH0OpjDdjImT0lu3hmuW/4al9U9ZtKT7baGCRSrDl2jdiY7om46OOZdIFdN92tO0gNPA==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-metamodel/-/concerto-metamodel-3.6.0.tgz", + "integrity": "sha512-5ojIrvq/hBAxwXH2Vwp5acVVgt11S76vQF3aWrOp4nN+PU8+H/kFYR26y19Pi3Ndwd5VWGRXzRVntEuNIrZrhQ==", "requires": { - "@accordproject/concerto-util": "3.2.0" + "@accordproject/concerto-util": "3.6.0" } }, "@accordproject/concerto-tools": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-tools/-/concerto-tools-3.2.0.tgz", - "integrity": "sha512-oxoPQoIL8PmIvgmiDUUXN3Ly0iUPpx30Oj3vSlO/Qaks/7Ywe/Y8yxCQk40kZWecQKyCQgk0K5fDNHG4ctaWlg==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-tools/-/concerto-tools-3.6.0.tgz", + "integrity": "sha512-pbN3m+9TMUJAJKbuWIaVfypIscMkc16hWtPZrMgiWslYTC75NA3HIu/Uk0JPL+sBq3/xZ8t7yKS3llcIaRBbrg==", "requires": { - "@accordproject/concerto-core": "3.2.0", - "@accordproject/concerto-util": "3.2.0", + "@accordproject/concerto-core": "3.6.0", + "@accordproject/concerto-util": "3.6.0", "ajv": "8.10.0", "ajv-formats": "2.1.1", "camelcase": "6.3.0", @@ -4257,9 +4240,9 @@ } }, "@accordproject/concerto-util": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@accordproject/concerto-util/-/concerto-util-3.2.0.tgz", - "integrity": "sha512-evzBWY+BLPSVgB1OgGKhT2cIXz6kZAhPqG0yWjaHAyUJYpvyN0PmBzmc4bI5AiL5RUseyBVkArfl2IAE9jz49w==", + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/@accordproject/concerto-util/-/concerto-util-3.6.0.tgz", + "integrity": "sha512-HLJuXoQkVmOfvr7nZ/0cDkSnJCL2nQeQ21tdDIpg2jJohIbzPzBSHTCltuUU5TBEK2tkLLjNXymeQD6PNFmSSg==", "requires": { "@supercharge/promise-pool": "1.7.0", "axios": "0.23.0", @@ -4785,14 +4768,6 @@ "@jridgewell/sourcemap-codec": "1.4.14" } }, - "@openapi-contrib/openapi-schema-to-json-schema": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/@openapi-contrib/openapi-schema-to-json-schema/-/openapi-schema-to-json-schema-3.2.0.tgz", - "integrity": "sha512-Gj6C0JwCr8arj0sYuslWXUBSP/KnUlEGnPW4qxlXvAl543oaNQgMgIgkQUA6vs5BCCvwTEiL8m/wdWzfl4UvSw==", - "requires": { - "fast-deep-equal": "^3.1.3" - } - }, "@sinonjs/commons": { "version": "1.8.5", "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.8.5.tgz", @@ -6033,14 +6008,6 @@ } } }, - "json-schema-migrate": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/json-schema-migrate/-/json-schema-migrate-2.0.0.tgz", - "integrity": "sha512-r38SVTtojDRp4eD6WsCqiE0eNDt4v1WalBXb9cyZYw9ai5cGtBwzRNWjHzJl38w6TxFkXAIA7h+fyX3tnrAFhQ==", - "requires": { - "ajv": "^8.0.0" - } - }, "json-schema-traverse": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", diff --git a/packages/concerto-cli/package.json b/packages/concerto-cli/package.json index b374716f9e..f880252604 100644 --- a/packages/concerto-cli/package.json +++ b/packages/concerto-cli/package.json @@ -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" }, diff --git a/packages/concerto-cli/test/cli.js b/packages/concerto-cli/test/cli.js index da06b541e5..96376d466e 100644 --- a/packages/concerto-cli/test/cli.js +++ b/packages/concerto-cli/test/cli.js @@ -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'); @@ -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, + 'person@1.0.0.Person', + '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, + 'person@1.0.0.Person2', + 'sample', + { offline: true, optionalFields: true } + ); + (typeof obj.ssn).should.equal('string'); + }); }); }); diff --git a/packages/concerto-cli/test/models/person.cto b/packages/concerto-cli/test/models/person.cto new file mode 100644 index 0000000000..3996e53c00 --- /dev/null +++ b/packages/concerto-cli/test/models/person.cto @@ -0,0 +1,19 @@ +namespace person@1.0.0 + +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 +} diff --git a/packages/concerto-core/lib/factory.js b/packages/concerto-core/lib/factory.js index 270782b84c..d6574c4d5e 100644 --- a/packages/concerto-core/lib/factory.js +++ b/packages/concerto-core/lib/factory.js @@ -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()); } diff --git a/packages/concerto-core/lib/serializer/instancegenerator.js b/packages/concerto-core/lib/serializer/instancegenerator.js index 4267da4c19..aa1ba3ec1a 100644 --- a/packages/concerto-core/lib/serializer/instancegenerator.js +++ b/packages/concerto-core/lib/serializer/instancegenerator.js @@ -160,7 +160,16 @@ class InstanceGenerator { let id = null; if (classDeclaration.isIdentified()) { - id = this.generateRandomId(classDeclaration); + let idFieldName = classDeclaration.getIdentifierFieldName(); + let idField = classDeclaration.getProperty(idFieldName); + if (idField?.isTypeScalar?.()){ + idField = idField.getScalarField(); + } + if(idField?.validator?.regex){ + id = parameters.valueGenerator.getRegex(fieldOrScalarDeclaration.validator.regex); + } else { + id = this.generateRandomId(classDeclaration); + } } let resource = parameters.factory.newResource(classDeclaration.getNamespace(), classDeclaration.getName(), id); parameters.stack.push(resource); diff --git a/packages/concerto-core/test/models/test.js b/packages/concerto-core/test/models/test.js index af00f20262..76ba6aba0b 100644 --- a/packages/concerto-core/test/models/test.js +++ b/packages/concerto-core/test/models/test.js @@ -53,7 +53,7 @@ describe('Test Model', function(){ // create a new instance let cObject = factory.newResource( - 'org.acme', 'Vehicle', 'CAR_123' ); + 'org.acme', 'Vehicle', 'YVBVLSFXXAL342374' ); // model is defined as a string // set model to a number @@ -61,10 +61,10 @@ describe('Test Model', function(){ cObject.model.should.equal('CAPRI'); // now try some invalid values - ( function() {cObject.setPropertyValue('model', 1);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "1" (type of value: "number"). Expected type of value: "String".'); - ( function() {cObject.setPropertyValue('model', true);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "true" (type of value: "boolean"). Expected type of value: "String".'); + ( function() {cObject.setPropertyValue('model', 1);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "1" (type of value: "number"). Expected type of value: "String".'); + ( function() {cObject.setPropertyValue('model', true);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "true" (type of value: "boolean"). Expected type of value: "String".'); ( function() {cObject.setPropertyValue('model', dayjs.utc());}).should.throw(/.+Expected type of value: "String"./); - ( function() {cObject.setPropertyValue('model', [1,2,3]);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "[1,2,3]" (type of value: "object"). Expected type of value: "String".'); + ( function() {cObject.setPropertyValue('model', [1,2,3]);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "[1,2,3]" (type of value: "object"). Expected type of value: "String".'); }); }); @@ -161,12 +161,12 @@ describe('Test Model', function(){ // create a new instance let factory = new Factory(modelManager); let cObject = factory.newResource( - 'org.acme', 'Vehicle', 'CAR_123' ); + 'org.acme', 'Vehicle', 'YVBVLSFXXAL342374' ); // vin is the identifying field for Vehicles, so should have been // set during object creation - cObject.vin.should.equal('CAR_123'); - cObject.getFullyQualifiedIdentifier().should.equal('org.acme.Vehicle#CAR_123'); + cObject.vin.should.equal('YVBVLSFXXAL342374'); + cObject.getFullyQualifiedIdentifier().should.equal('org.acme.Vehicle#YVBVLSFXXAL342374'); cObject.make = 'Renault'; @@ -190,15 +190,15 @@ describe('Test Model', function(){ // model is defined as a string // set model to a number cObject.model = 1; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "1" (type of value: "number"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "1" (type of value: "number"). Expected type of value: "String".'); // set model to a double cObject.model = 42.05; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "42.05" (type of value: "number"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "42.05" (type of value: "number"). Expected type of value: "String".'); // set model to a Boolean cObject.model = true; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "true" (type of value: "boolean"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "true" (type of value: "boolean"). Expected type of value: "String".'); // set model to a DateTime cObject.model = dayjs.utc(); @@ -206,19 +206,19 @@ describe('Test Model', function(){ // set model to an object cObject.model = { 'foo' : 'bar' }; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "{"foo":"bar"}" (type of value: "object"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "{"foo":"bar"}" (type of value: "object"). Expected type of value: "String".'); // set model to null cObject.model = null; - ( function() {serializer.toJSON(cObject);}).should.throw('The instance "org.acme.Vehicle#CAR_123" is missing the required field "model".'); + ( function() {serializer.toJSON(cObject);}).should.throw('The instance "org.acme.Vehicle#YVBVLSFXXAL342374" is missing the required field "model".'); // set model to an array cObject.model = ['1','2']; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "["1","2"]" (type of value: "object"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "["1","2"]" (type of value: "object"). Expected type of value: "String".'); // set model to a function cObject.model = function() {throw new Error('OOps');}; - ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#CAR_123" instance. The field "model" has a value of "undefined" (type of value: "function"). Expected type of value: "String".'); + ( function() {serializer.toJSON(cObject);}).should.throw('Model violation in the "org.acme.Vehicle#YVBVLSFXXAL342374" instance. The field "model" has a value of "undefined" (type of value: "function"). Expected type of value: "String".'); }); }); diff --git a/packages/concerto-core/test/serializer/instancegenerator.js b/packages/concerto-core/test/serializer/instancegenerator.js index edbf7cd5b4..5538ac92e0 100644 --- a/packages/concerto-core/test/serializer/instancegenerator.js +++ b/packages/concerto-core/test/serializer/instancegenerator.js @@ -443,6 +443,17 @@ describe('InstanceGenerator', () => { resource.ssn.should.match(/^\d{3}-\d{2}-\d{4}$/); resource.ssn2.should.equal('000-00-0000'); }); + + it('should throw an error when id provided does not match regex on id field', function () { + (() => test(`namespace org.acme.test + + scalar SSN extends String regex=/^\\d{3}-\\d{2}-\\d{4}$/ + + asset MyAsset identified by ssn { + o String id + o SSN ssn + }`)).should.throw(/Provided id does not match regex/); + }); }); describe('#findConcreteSubclass', () => {