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

feat(stringvalidator): client provided regex #487

Merged
merged 3 commits into from
Aug 16, 2022
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
15 changes: 0 additions & 15 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class AstModelManager extends BaseModelManager {
+ void constructor(object?)
}
class BaseModelManager {
+ void constructor(object?,boolean?,processFile?)
+ void constructor(object?,boolean?,Object?,processFile?)
+ boolean isModelManager()
+ boolean isVersionedNamespacesStrict()
+ Object accept(Object,Object)
Expand Down Expand Up @@ -244,7 +244,7 @@ class ModelLoader {
+ ModelManager[] loadModelManagerFromModelFiles(object[],string[],object,boolean?,boolean?,number?)
}
class ModelManager extends BaseModelManager {
+ void constructor(object?,boolean?)
+ void constructor(object?,boolean?,Object?)
+ ModelFile addCTOModel(string,string?,boolean?) throws IllegalModelException
}
+ object getRootModel()
Expand Down
4 changes: 4 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 3.0.0 {b0c9b0951c58e3deeed8fbccf2dfd052} 2022-08-16
- Allow client-provided RegExp engine to ModelManager

Version 2.3.0 {774980a857090905fe276b6e94f1dbb1} 2022-07-21
- Versioned namespaces

Expand Down
2 changes: 2 additions & 0 deletions packages/concerto-core/lib/basemodelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class BaseModelManager {
* @constructor
* @param {object} [options] - ModelManager options, also passed to Serializer
* @param {boolean} [options.versionedNamespacesStrict] - require versioned namespaces and imports
* @param {Object} [options.regExp] - An alternative regular expression engine.
* @param {*} [processFile] - how to obtain a concerto AST from an input to the model manager
*/
constructor(options, processFile) {
Expand All @@ -84,6 +85,7 @@ class BaseModelManager {
this.serializer = new Serializer(this.factory, this, options);
this.decoratorFactories = [];
this.versionedNamespacesStrict = !!options?.versionedNamespacesStrict;
this.options = options;
this.addRootModel();
}

Expand Down
7 changes: 2 additions & 5 deletions packages/concerto-core/lib/introspect/stringvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

'use strict';

const { RE2 } = require('re2-wasm');
const Validator = require('./validator');

// Types needed for TypeScript generation.
Expand All @@ -37,16 +36,14 @@ class StringValidator extends Validator{
* Create a StringValidator.
* @param {Field} field - the field this validator is attached to
* @param {Object} validator - The validation string. This must be a regex
* expression.
*
* @throws {IllegalModelException}
*/
constructor(field, validator) {
super(field, validator);
try {
// RE2 must always be run in unicode mode, it's safe to add 'u' even if it already exists
const flags = validator.flags ? validator.flags + 'u' : 'u';
this.regex = new RE2(validator.pattern, flags);
const CustomRegExp = field?.parent?.getModelFile()?.getModelManager()?.options?.regExp || RegExp;
this.regex = new CustomRegExp(validator.pattern, validator.flags);
}
catch(exception) {
this.reportError(field.getName(), exception.message);
Expand Down
1 change: 1 addition & 0 deletions packages/concerto-core/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ModelManager extends BaseModelManager {
* @constructor
* @param {object} [options] - ModelManager options, also passed to Serializer
* @param {boolean} [options.versionedNamespacesStrict] - require versioned namespaces and imports
* @param {Object} [options.regExp] - An alternative regular expression engine.
*/
constructor(options) {
super(options, ctoProcessFile(options));
Expand Down
74 changes: 59 additions & 15 deletions packages/concerto-core/package-lock.json

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

2 changes: 1 addition & 1 deletion packages/concerto-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"typescript": "4.6.3",
"webpack": "5.64.2",
"webpack-cli": "4.9.1",
"xregexp": "5.1.1",
"yargs": "17.3.1"
},
"dependencies": {
Expand All @@ -74,7 +75,6 @@
"debug": "4.3.1",
"lorem-ipsum": "2.0.3",
"randexp": "0.5.3",
"re2-wasm": "1.0.2",
"semver": "7.3.5",
"slash": "3.0.0",
"urijs": "1.19.11",
Expand Down
22 changes: 0 additions & 22 deletions packages/concerto-core/re2.js

This file was deleted.

20 changes: 19 additions & 1 deletion packages/concerto-core/test/introspect/stringvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require('chai').should();
const sinon = require('sinon');
const NumberValidator = require('../../lib/introspect/numbervalidator');

const XRegExp = require('xregexp');

describe('StringValidator', () => {

let mockField;
Expand All @@ -44,7 +46,7 @@ describe('StringValidator', () => {

it('should ignore a null string', () => {
let v = new StringValidator(mockField, { pattern: '^[A-z][A-z][0-9]{7}' });
v.getRegex().toString().should.equal('/^[A-z][A-z][0-9]{7}/u');
v.getRegex().toString().should.equal('/^[A-z][A-z][0-9]{7}/');
v.validate('id', null);
});

Expand Down Expand Up @@ -87,6 +89,22 @@ describe('StringValidator', () => {

});

describe('#validate with custom RegEx engine', () => {
const options = {
regExp: XRegExp
};
it('should ignore a null string', () => {
let v = new StringValidator(mockField, { pattern: '^[A-z][A-z][0-9]{7}' }, options);
v.getRegex().toString().should.equal('/^[A-z][A-z][0-9]{7}/');
v.validate('id', null);
});

it('should validate a string', () => {
let v = new StringValidator(mockField, { pattern: '^[\\p{Letter}\\p{Number}]{7}', flags: 'u' }, options);
v.validate('id', 'AB1234567');
});
});

describe('#compatibleWith', () => {
it('should return false for a number validator', () => {
const other = new NumberValidator(mockField, { lower: -1, upper: 1 });
Expand Down
21 changes: 21 additions & 0 deletions packages/concerto-core/test/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const fs = require('fs');
const XRegExp = require('xregexp');

const FileDownloader = require('@accordproject/concerto-util').FileDownloader;
const AssetDeclaration = require('../lib/introspect/assetdeclaration');
Expand All @@ -27,6 +28,7 @@ const ModelFile = require('../lib/introspect/modelfile');
const ModelManager = require('../lib/modelmanager');
const ParticipantDeclaration = require('../lib/introspect/participantdeclaration');
const Serializer = require('../lib/serializer');
const Concerto = require('../lib/concerto');
const TypeNotFoundException = require('../lib/typenotfoundexception');
const Util = require('./composer/composermodelutility');
const COMPOSER_MODEL = require('./composer/composermodel');
Expand Down Expand Up @@ -186,6 +188,25 @@ describe('ModelManager', () => {
JSON.stringify(ast).should.not.contain('location');
});

it('should add a model file with alternative RegExp option', () => {
XRegExp.install('astral');
const regExp = XRegExp;
const modelManagerWithOptions = new ModelManager({ regExp });
Util.addComposerModel(modelManagerWithOptions);

modelManagerWithOptions.addCTOModel(`namespace org.acme
concept Bar {
o String foo regex=/\\p{S}/
}`, 'internal.cto', true);

const bar = {
$class : 'org.acme.Bar',
foo : '😊'
};
const concerto = new Concerto(modelManagerWithOptions);
concerto.validate(bar);
});

it('should return error for duplicate namespaces for a string', () => {
modelManager.addCTOModel(modelBase);
let mf1 = sinon.createStubInstance(ModelFile);
Expand Down
Loading