From 5a458ec5c88ca369c85ea99f641863d9099bd90d Mon Sep 17 00:00:00 2001 From: Gauntlet173 Date: Tue, 18 Jul 2023 17:03:47 -0600 Subject: [PATCH 1/2] input validation added and docs changed --- .../docs/blocks/categories/new_attribute.yaml | 12 +++- .../docs/blocks/categories/new_category.yaml | 15 +++- .../blocks/categories/new_relationship.yaml | 12 +++- .../docs/blocks/objects/new_object.yaml | 13 +++- .../docs/blocks/variables/variable.yaml | 13 +++- blawx/fixtures/docs/features/categories.yaml | 65 +++++++++--------- blawx/fixtures/docs/features/variables.yaml | 9 ++- blawx/static/blawx/blawx-blocks.js | 68 ++++++++++++++++++- 8 files changed, 162 insertions(+), 45 deletions(-) diff --git a/blawx/fixtures/docs/blocks/categories/new_attribute.yaml b/blawx/fixtures/docs/blocks/categories/new_attribute.yaml index ac914da2..d9c2c406 100644 --- a/blawx/fixtures/docs/blocks/categories/new_attribute.yaml +++ b/blawx/fixtures/docs/blocks/categories/new_attribute.yaml @@ -22,8 +22,6 @@ It accepts a category to which the attribute should be applied, specified in a dropdown, a name for the attribute specified in a text field, and an attribute type specified in a dropdown. - To be valid, an attribute name should start with a lowercase letter and not have any spaces. - The datatype can be a scalar data type like "number", or another category. The block also accepts an order for how the attribute should be displayed in natural language, @@ -39,6 +37,16 @@ ## Tips + To be valid, an attribute name must + + * contain only numbers, underscores, and letters + * begin with a lowercase letter + * not end in an underscore followed by only digits + + The block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial uppercase letter into lowercase. Other invalid values will be rejected, and + the value of the field will not be changed. + Note that if you specify a data type as the attribute type your attribute selector blocks will only accept the correct type of value, and variables in the value position. If you choose a category as the data type, your attribute selector blocks will accept any object, or a variable. diff --git a/blawx/fixtures/docs/blocks/categories/new_category.yaml b/blawx/fixtures/docs/blocks/categories/new_category.yaml index b68b98c1..69d9ede6 100644 --- a/blawx/fixtures/docs/blocks/categories/new_category.yaml +++ b/blawx/fixtures/docs/blocks/categories/new_category.yaml @@ -19,12 +19,21 @@ The new category block is a statement, and can be stacked with other statements. - The new category block accepts the name of the category. To be valid, the name of the category must begin with a lower-case letter and not - include any spaces. + The new category block accepts the name of the category. It also accepts text that should appear before and after the name of the object in explanations. ## Tips Names of categories should be unique, but can safely - overlap with the names of non-boolean attributes. \ No newline at end of file + overlap with the names of non-boolean attributes. + + To be valid, a category name must + + * contain only numbers, underscores, and letters + * begin with a lowercase letter + * not end in an underscore followed by only digits + + The block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial uppercase letter into lowercase. Other invalid values will be rejected, and + the value of the field will not be changed. \ No newline at end of file diff --git a/blawx/fixtures/docs/blocks/categories/new_relationship.yaml b/blawx/fixtures/docs/blocks/categories/new_relationship.yaml index afd565cf..bb718b4a 100644 --- a/blawx/fixtures/docs/blocks/categories/new_relationship.yaml +++ b/blawx/fixtures/docs/blocks/categories/new_relationship.yaml @@ -32,4 +32,14 @@ ## Tips - Names of relationships should be unique, but can safely overlap with other relationship that have a different number of objects and values. \ No newline at end of file + Names of relationships should be unique, but can safely overlap with other relationship that have a different number of objects and values. + + To be valid, a relationship name must + + * contain only numbers, underscores, and letters + * begin with a lowercase letter + * not end in an underscore followed by only digits + + The block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial uppercase letter into lowercase. Other invalid values will be rejected, and + the value of the field will not be changed. \ No newline at end of file diff --git a/blawx/fixtures/docs/blocks/objects/new_object.yaml b/blawx/fixtures/docs/blocks/objects/new_object.yaml index fdb6bf74..dff4933c 100644 --- a/blawx/fixtures/docs/blocks/objects/new_object.yaml +++ b/blawx/fixtures/docs/blocks/objects/new_object.yaml @@ -28,8 +28,17 @@ The new object block asserts that the named object is in the category. - The text field must be the name of the object, which must not include spaces - and must start with a lower-case letter. Names for objects must also be unique. + The text field must be the name of the object. Names for objects must also be unique. + + To be valid, an object name must + + * contain only numbers, underscores, and letters + * begin with a lowercase letter + * not end in an underscore followed by only digits + + The block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial uppercase letter into lowercase. Other invalid values will be rejected, and + the value of the field will not be changed. The new object block is a statement, and can be stacked with other statements. diff --git a/blawx/fixtures/docs/blocks/variables/variable.yaml b/blawx/fixtures/docs/blocks/variables/variable.yaml index eb1867a3..b6c88b21 100644 --- a/blawx/fixtures/docs/blocks/variables/variable.yaml +++ b/blawx/fixtures/docs/blocks/variables/variable.yaml @@ -23,8 +23,17 @@ ## Technical Details - The variable block requires you to enter a variable name into the text field, - which must not include spaces and must start with a capital letter to be valid. + The variable block requires you to enter a variable name into the text field. + + To be valid, a variable name must + + * contain only numbers, underscores, and letters + * begin with an uppercase letter + * not end in an underscore followed by only digits + + The block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial lowercase letter into uppercase. Other invalid values will be rejected, and + the value of the field will not be changed. The variable block can be entered anywhere an object or a value is expected. diff --git a/blawx/fixtures/docs/features/categories.yaml b/blawx/fixtures/docs/features/categories.yaml index 21b4c55c..29c4fea4 100644 --- a/blawx/fixtures/docs/features/categories.yaml +++ b/blawx/fixtures/docs/features/categories.yaml @@ -28,9 +28,9 @@ An easy way to think of a Category is as a description of a blank form. The form might have - a name, like “Application for a Permit”. That is like the Category name. A form - will also have fields that can be filled in, like “Applicant’s Name". Those are - like the category’s attributes. + a name, like "Application for a Permit". That is like the Category name. A form + will also have fields that can be filled in, like "Applicant's Name". Those are + like the category's attributes. When you create and fill out an actual form of that type, that is like creating an Object. The information that gets @@ -44,7 +44,7 @@ An attribute or a relationship can hold either a basic data type, or objects in a category. - The basic data types are numbers, true or false values, dates, times, datetimes, durations, and lists. + The basic data types are numbers, values, dates, times, datetimes, durations, and lists. An attribute can also be true or false. Not only can you use your Categories as a type, but you can use your category @@ -59,7 +59,7 @@ ### Creating Categories You create a category by putting a category declaration block in a fact block. A - category declaration block can be found under the “Categories” drawer in the + category declaration block can be found under the "Categories" drawer in the Toolbox. ![Person is Category](/static/blawx/docs/images/person_is_category.png) @@ -68,13 +68,14 @@ and in explanations. In this case, the phrase will be "_____ is a person". Category names should not - have spaces in them, and must start with a lowercase letter. Blawx will not stop you from creating - a category with a space in its name, but they will not work properly. + have spaces in them, must start with a lowercase letter, must contain only letters, numerals, and underscores, + and must not end in an underscore followed by numerals. Blawx will automatically correct some problems like these + for you, but other invalid category names will be rejected. ### Adding Attributes to a Category Once you have declared a Category, you can use an attribute declaration block to give it an attribute. The attribute declaration - block can be found in the “Categories” drawer of the toolbox, and looks like + block can be found in the "Categories" drawer of the toolbox, and looks like this: ![attribute declaration binary](/static/blawx/docs/images/new_attribute_declaration_binary.png) @@ -83,13 +84,13 @@ a data type, and then describe how it should be phrased in attribute selector blocks and explanations. For - example, to say that “a person has an age and a best friend” you might create + example, to say that "a person has an age and a best friend" you might create attribute declaration blocks like these: ![person ontology](/static/blawx/docs/images/person_ontology.png) - The name of an attribute must start with a lowercase letter, must not have spaces, - and must be unique across all categories. + The name of an attribute must follow the same rules as the name of a category. It also needs to be unique + across all categories. ### Attribute Customization @@ -152,26 +153,26 @@ ![bob is person](/static/blawx/docs/images/bob_is_person.png) - Object names should start with lowercase letters, and should not have spaces in them. - Blawx will not stop you from creating - an object with a name that has spaces - in it, but it won't work properly. + Object names need to follow the same rules as category and attribute names, and should be unique. + Blawx will correct some errors in object names, such as removing leading and trailing spaces, + converting other spaces to underscores, and making the first letter lowercase. Other errors + will result in the changed value being rejected. When you drag an object declaration - onto the Workspace, a corresponding Object Block appears in the “Known Objects” + onto the Workspace, a corresponding Object Block appears in the "Known Objects" drawer of the Toolbox. ![bob known](/static/blawx/docs/images/known_object_bob.png) The - object block for “Bob” can be used anywhere a block is expecting an or accepts + object block for "Bob" can be used anywhere a block is expecting an or accepts an object as a value. ### Giving an Object another Category All objects are created inside a category. But it is possible to assign additional categories to objects that have already been created. You can do this using the object category block, which can be found in - the “Objects” drawer of the Toolbox. It expects an object block and a category + the "Objects" drawer of the Toolbox. It expects an object block and a category block. For example, if we wanted to say that in addition to being in the category person, "bob" is in the category "mammal", we could create a statement like this: @@ -182,21 +183,21 @@ from more than one source, and each source may create its own versions of the same objects. In those cases, it can be useful to state that two objects are the same object. You can do that using the object equivalence block, which is - found in the “Objects” drawer of the Toolbox, and which looks like this: + found in the "Objects" drawer of the Toolbox, and which looks like this: ![object identity](/static/blawx/docs/images/blocks/object_equality.png) The object identity - block takes two object blocks. To say that “Bob” and “Robert” are the same object, + block takes two object blocks. To say that "Bob" and "Robert" are the same object, you could do this: ![bob and robert](/static/blawx/docs/images/bob_and_robert_identical.png) Note that object equivalence goes in both directions. If Bob is the same object as Robert, - Robert is the same object as Bob. So if you set out Bob’s age, and then - say that Bob and Robert are the same object, asking for Bob’s age and - asking for Robert’s age will both work. + Robert is the same object as Bob. So if you set out Bob's age, and then + say that Bob and Robert are the same object, asking for Bob's age and + asking for Robert's age will both work. ### Adding Attributes to an Object @@ -211,20 +212,21 @@ ### Naming Attributes - Attribute names must start with a lowercase letter, and must not have spaces in them. - They must also be unique across all categories. - Blawx will not currently - stop you from creating invalid attribute names, but they will cause errors. + Attribute names must contain only letters, numerals, and underscores, must start with + a lowercase letter, and must not end with an underscore followed by numerals. Blawx will + remove leading and trailing spaces, convert internal spaces to underscores, and make the + first letter of your attribute name lowercase for you. Names that are invalid in other ways + will be rejected. When working with attributes you will want to be careful with how you name them. Blawx will not stop you from - applying an attribute defined for “cars” to an object in the category “bicycle”. + applying an attribute defined for "cars" to an object in the category "bicycle". If you need to know how many seats both categories have, but you want to avoid the implication that bikes have car seats or vice-versa, you may want to name - the attributes “bike_seats” and “car_seats” to make it clear what you are referring + the attributes "bike_seats" and "car_seats" to make it clear what you are referring to. - If you create two attributes both named “seats”, they will both appear + If you create two attributes both named "seats", they will both appear in the Known Attributes drawer of the toolbox, but Blawx will treat them as two copies of the same attribute. @@ -247,7 +249,8 @@ ## Relationships You can create a relationship by using the new relationship block. You need to give it a name, which - must be lower case and not have spaces. You also need to give it a number of objects or values that will + must follow the same naming conventions for categories, attributes and objects. + You also need to give it a number of objects or values that will be related to one another. Note that if what you are looking for is a relationship between 2 objects, you should use an Attribute instead. diff --git a/blawx/fixtures/docs/features/variables.yaml b/blawx/fixtures/docs/features/variables.yaml index 535f62db..8988baad 100644 --- a/blawx/fixtures/docs/features/variables.yaml +++ b/blawx/fixtures/docs/features/variables.yaml @@ -59,7 +59,12 @@ the name of the variable, but you can use any other name you like. **Note that - variable names should not include spaces, and must start with a capital letter.** + variable names should include only letters, numerals, and underscores, must start with a capital letter, and must not end + in an underscore followed by numerals.** + + The variable block will automatically remove leading and trailing spaces, convert spaces to underscores, + and convert an initial lowercase letter into uppercase. Other invalid variable names will be rejected, and + the variable name will not be changed. ### Variables in Rule Conclusions @@ -147,7 +152,7 @@ A named variable is the only kind of variable that will cause Blawx to give you the specific value in response to a question, outside of the explanation. - A variable should start with a capital letter, and have no spaces. + See above for the rules on what counts as a valid variable name. ### Unnamed Variables diff --git a/blawx/static/blawx/blawx-blocks.js b/blawx/static/blawx/blawx-blocks.js index 93409b45..e2b16b76 100644 --- a/blawx/static/blawx/blawx-blocks.js +++ b/blawx/static/blawx/blawx-blocks.js @@ -5120,9 +5120,36 @@ scasp_blockset = [{ "helpUrl": "/docs/blocks/relationship_selector/" }] +Blockly.Extensions.register('variable_name_validator', + function() { + var field = this.getField('variable_name'); + field.setValidator(validate_variable); + } +) + +Blockly.Extensions.register('category_name_validator', + function() { + var field = this.getField('category_name'); + field.setValidator(validate_predicate); + } +) + +Blockly.Extensions.register('object_name_validator', + function() { + var field = this.getField('object_name'); + field.setValidator(validate_predicate); + } +) + // TODO: A bunch of these things below are redundant as the blocks are being removed. // Make modifications that it is not possible to make in the Developer Tools for (var i = 0; i < scasp_blockset.length; i++) { + if (scasp_blockset[i].type == "new_category_declaration") { + scasp_blockset[i]['extensions'] = ['category_name_validator']; + } + if (scasp_blockset[i].type == "variable") { + scasp_blockset[i]['extensions'] = ['variable_name_validator']; + } if (scasp_blockset[i].type == "attribute_selector") { scasp_blockset[i]['mutator'] = "attribute_selector_mutator" }; @@ -5157,6 +5184,7 @@ for (var i = 0; i < scasp_blockset.length; i++) { } if (scasp_blockset[i].type == "object_declaration") { scasp_blockset[i]['mutator'] = "object_declaration_mutator"; + scasp_blockset[i]['extensions'] = ['object_name_validator']; }; // if (scasp_blockset[i].type == "category_display") { // scasp_blockset[i]['extensions'] = ['changeCategoryDisplayText']; @@ -5185,10 +5213,46 @@ for (var i = 0; i < scasp_blockset.length; i++) { var headless=false; +function validate_predicate(predicate_name) { + // make sure the first letter is a letter + if (predicate_name[0].toLowerCase().search(/[a-z]/) == 0) { + // make sure it does not end in a _ followed by digits + if (predicate_name.replaceAll(' ','_').search(/_\d+$/) == -1) { + // make sure all the characters are letters, numbers, or spaces + if (predicate_name.search(/^[\w ]*$/) != -1) { + // trim leading and trailing spaces, replace other spaces with underscores, and lowercase + var output = predicate_name[0].toLowerCase() + output += predicate_name.slice(1).trim().replaceAll(' ','_') + return output; + } + } + } + // the first character is not a letter, or it contains symbols, or it ends in an _# pattern. + return null; +} + +function validate_variable(variable_name) { + // make sure the first letter is a letter + if (variable_name[0].toUpperCase().search(/[A-Z]/) == 0) { + // make sure it does not end in a _ followed by digits + if (variable_name.replaceAll(' ','_').search(/_\d+$/) == -1) { + // make sure all the characters are letters, numbers, or spaces + if (variable_name.search(/^[\w ]*$/) != -1) { + // trim leading and trailing spaces, replace other spaces with underscores, and uppercase the first letter + var output = variable_name[0].toUpperCase() + output += variable_name.slice(1).trim().replaceAll(' ','_') + return output; + } + } + } + // the first character is not a letter, or it contains symbols, or it ends in an _# pattern. + return null; +} + Blockly.Blocks['relationship_declaration'] = { init: function() { this.appendDummyInput("topline") - .appendField(new Blockly.FieldTextInput("relationship_name"), "relationship_name") + .appendField(new Blockly.FieldTextInput("relationship_name",validate_predicate), "relationship_name") .appendField("is a relationship between") .appendField(new Blockly.FieldDropdown([["3","3"],["4","4"],["5","5"],["6","6"],["7","7"],["8","8"],["9","9"],["10","10"]]), "relationship_arity") .appendField("objects or values, displayed as"); @@ -5306,7 +5370,7 @@ Blockly.Blocks['new_attribute_declaration'] = { .appendField("The category") .appendField(new Blockly.FieldDropdown([['no categories defined','none']]),"category_name") .appendField("has an attribute") - .appendField(new Blockly.FieldTextInput("attribute name"), "attribute_name"); + .appendField(new Blockly.FieldTextInput("attribute name",validate_predicate), "attribute_name"); this.appendDummyInput() .appendField("which is of type") .appendField(new Blockly.FieldDropdown([["true / false","boolean"], ["number","number"], ["date","date"], ["time","time"], ["datetime","datetime"], ["duration","duration"], ['list','list']]),"attribute_type") From 75beeb85ad421488ab32321d584935c0aab39b62 Mon Sep 17 00:00:00 2001 From: Gauntlet173 Date: Tue, 18 Jul 2023 17:04:18 -0600 Subject: [PATCH 2/2] updating changelog and version number --- CHANGELOG.md | 21 +++++++++++++++++++++ blawx/settings.py | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28fa7ab6..a3ef9c77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,27 @@ As of v0.2-alpha, this project is attempting to adhere to [Semantic Versioning]( While alpha, however, any version may include breaking changes that may not be specifically noted as such, and breaking changes will not necessarily result in changes to the main version number. +## [v1.6.17-alpha](https://github.com/Lexpedite/blawx/releases/tag/v1.6.17-alpha) 2023-07-18 + +This release adds validation for category names, attribute names, object names, relationship names, and variable names. +The corresponding blocks will now convert invalid names to valid names where possible, and will reject names that they +cannot convert. + +This version is reverse compatible with .blawx files from previous versions. However, invalid names will be either corrected +if possible, or if not possible to correct will be replaced with the default value for the relevant field. This may have unexpected +effects in encodings with invalid field values. + +### Changed +* New Relationship, New Category, New Attribute, New Object, and Variable blocks now validate and correct + names as follows: + * first letters will be corrected to uppercase for variables and lowercase for all others + * spaces at the start and end will be removed + * spaces elsewhere will be converted to underscores + * names with symbols other than letters, numerals, underscore, and space will be rejected + * names that end in an underscore followed by numerals will be rejected +* Documentation has been updated to reflect the new behaviour. + + ## [v1.6.16-alpha](https://github.com/Lexpedite/blawx/releases/tag/v1.6.16-alpha) 2023-06-26 diff --git a/blawx/settings.py b/blawx/settings.py index d6c9db78..30457363 100644 --- a/blawx/settings.py +++ b/blawx/settings.py @@ -13,7 +13,7 @@ from pathlib import Path # For adding a version identifier -BLAWX_VERSION = "v1.6.16-alpha" +BLAWX_VERSION = "v1.6.17-alpha" # Build paths inside the project like this: BASE_DIR / 'subdir'.