Skip to content

Commit

Permalink
Restrict owned public entities
Browse files Browse the repository at this point in the history
PR-URL: #404
  • Loading branch information
nechaido authored and belochub committed Apr 25, 2019
1 parent 92a99c8 commit a065b6f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/metaschema-config/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ const {
HIERARCHICAL_RELATIONS,
} = require('./utils');

const preprocessCategoryData = entity => {
const preprocessCategoryData = (type, entity) => {
const [first, second] = entity.name.split('.');
if (!second) {
if (entity.definition.Public) {
return first && second
? [new SchemaValidationError('ownedPublic', entity.name, { type })]
: [];
} else if (!second) {
entity.category = entity.module;
} else {
entity.category = first;
Expand Down
7 changes: 5 additions & 2 deletions lib/metaschema-config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ SchemaValidationError.serializers.illegalParamsResolving = ({
info: { type },
}) => `Params resolving is forbidden for ${type}: '${source}'`;

SchemaValidationError.serializers.ownedPublic = ({ source, info: { type } }) =>
`Public ${type}: '${source}' can not be defined on a category`;

const preprocessor = schemas => {
const arr = [...schemas];

Expand Down Expand Up @@ -141,14 +144,14 @@ module.exports = {
creator: categoryCreator,
},
action: {
preprocess: [preprocessCategoryData],
preprocess: [preprocessCategoryData.bind(null, 'action')],
add: [addAction],
postprocess: [processAction],
validateInstance: validateAction,
creator: actionCreator,
},
form: {
preprocess: [preprocessCategoryData, preprocessForm],
preprocess: [preprocessCategoryData.bind(null, 'form'), preprocessForm],
add: [addForm],
postprocess: [processForm],
validateInstance: validateForm,
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/owned-public-action/Category.Action.action
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Action({
Public: true,
Execute: () => {},
});
29 changes: 29 additions & 0 deletions test/ms.owned-public-action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const metaschema = require('metaschema');
const metatests = require('metatests');

const { options, config } = require('../lib/metaschema-config/config');

const { MetaschemaError, SchemaValidationError } = metaschema.errors;

metatests.test('Fully supports schemas/system', async test => {
try {
await metaschema.fs.load(
'test/fixtures/owned-public-action',
options,
config
);
test.fail('Schema loading must fail');
} catch (error) {
test.strictSame(
error,
new MetaschemaError([
new SchemaValidationError('ownedPublic', 'Category.Action', {
type: 'action',
}),
])
);
}
test.end();
});

0 comments on commit a065b6f

Please sign in to comment.