Skip to content
Riccardo Cocetta edited this page Mar 15, 2018 · 18 revisions

Recently a new validator was written: here is how it works.

How to validate steps:

In most cases (where you have only a simple line), you'll need to just write the line in the solution key of the steps and the system will escape it in a way that it gets validated.

In case you need something more, than you can get the more advanced features using the validate key;

Validate

validate is either a single element or an array of either string or object

If an element is a string it'll be considered a regex, if it's an object it'll be validated depending on the type. For the moment we only have type: fn which will contain a function which can receive a string representing the line that it's validating.

**NOTE ** The prefix @@ tells the validator that it has to escape the string to be a RegExp. You might want to use @@ when validating against something like @@mosquito = (x,y) ->

If the array contains more than one element, the validator will check more than 1 line.

"steps": [
    {
     "solution":     "move 5, 10"
    },
    {
     "solution": "aaa",
     "validate": [
         { 
           "type": "function",
           "fn"  : "function (str) {return str.length===3;}"
         }
     ]
    },
    {
     "validate": [
         "color white",
         { 
           "type": "function", 
           "fn"  : "function (str) {return str.length===3;}"
         }
     ]
    },

]

Behaviour and Special cases

When you write a solution or validate string it'll be automatically escaped for

  • [ ] ..

  • ^ and $ added at beginning and end

  • Flexible whitespaces will be added for [] , .. and after the first command

  • if you prefix your rule with @@ all the regex characters will be escaped, but no other flexibility on the spaces etc. will be added (I don't know if this is still useful)

  • if you prefix your rule with __ double underscore it'll be considered a regex as it is and no transformation will be applied.

#Examples By starting to play with Regular Expressions one can make the validation a bit smarter: optional spaces, escaped characters etc.

Note: You can find more examples by looking at the "test" Directory (temporary note: only on the validator branch).

A good site to play with RegExes is https://regex101.com/

A simple move

Optional space needs a **

        {
            "hint": "Move down by 100 - **type** `move 0, 100`",
            "solution": "move 0, 100"
        }

Polygon

        {
            "hint": "Draw the knot - **type** `polygon 15, 15, -15, 15`",
            "solution": "polygon 15, 15, -15, 15"
        }

Array of checks

Less hints for more checks?

        {
            "hint": "Top it with a cheesy yellow circle of size 195",
            "solution": "color yellow\ncircle 195",
            "validate": [
                "color yellow",
                "circle 195"
            ]
        }

For loop

{
            "hint": "Now open a loop - **type** `for x in [ 0 .. 25 ]`",
            "solution": "for x in [ 0..25 ]"
        },
        {
            "hint": " NOTE ANOTHER WAY TO VALIDATE IT (the difference in spaces) Inside this loop,  open a second loop - **type** `for y in [ 0 .. 25 ]`",
            "solution": "    for y in [ 0 .. 25 ]",
            "validate": "    for y in [ 0..25 ]"
        },
        {
            "hint": "Move around, **type** `moveTo x * 20, y * 20`",
            "solution": "        moveTo x * 20, y * 20",
            "validate": "^        moveTo x *\\* *20, *y *\\* *20$"
        },
        {
            "hint": "Now for the dots - **type** `circle 6`",
            "solution": "        circle 6"
        }