Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-2898

Data validation functions - validjson() validdata()

    XMLWordPrintable

    Details

    • Story Points:
      1

      Description

      There is currently no easy way to handle JSON parsing errors in readjson() or parsejson().

      This is especially useful if a binary supposed to output json outputs an error message instead. When parsing it's output, you could use if => validjson() to validate first.

      Note that this requires 2 passes, so it is not ideal for large data files.

      Acceptance criteria:

      • validjson(string) returns true if the string contains valid json data, false otherwise.
      • validyaml(string) returns true if the string contains valid yaml data, false otherwise.
      • validcsv(string) returns true if the string contains valid data according to RFC 4180 (the spec that readcsv() follows http://www.ietf.org/rfc/rfc4180.txt)
      • validdata(string, type) returns true if the string contains valid data, according to type parser (json, yaml)

      The developer who implements this should note that all of these can be implemented as the same C function, similar to:

      https://github.com/cfengine/core/blob/master/libpromises/evalfunction.c#L6585

      (The underlying C function for readdata checks the name of policy function called, for readjson, readyaml, etc.) 

      Example Usage

      bundle agent main
      {
          vars:
            "f" string => "/path/to/data.json";
      
          # Read the data in if it's valid. 
            "data"
              data => readjson( $(f), inf),
              if => validjson( readfile( $(f), inf ) );
      }
      

        Attachments

          Activity

            People

            • Assignee:
              karlhto Karl Hole Totland
              Reporter:
              olehermanse Ole Herman Schumacher Elgesem
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Summary Panel