Any advise on JSON schema validator using Suitescr...
# suitescript
k
Any advise on JSON schema validator using Suitescript ?
n
what are you looking to validate?
k
Shopify order payload
We have schema and payload that comes from other systems
n
like what aspect of the schema do you need though
k
Validate all properties present in payload
in other words, make sure it has all keys that required to generate order
and also, make sure we are always receiving accurate payload
n
if you want to validate a key in the schema, you could do something like
Copy code
var x = Object.keys(context);
if (x.indexOf('keyname') != -1) {
// do something
}
this method pushes all the keys of the object to a list, which you then check to make sure key exists in list
k
does that works for nested keys and it's arrays
Copy code
{
  "Order": {
    "ID": 234,
    "items": [
      {
        "sku": "3454JJ",
        "quantity": 1
      },
      {
        "sku": "3454JJ",
        "quantity": 1
      }
    ]
  }
}
n
you would have to index into the nested objects etc so instead of context, you could do context.items.length() to see if there is anything in the items array
r
We use ajv for JSON Schema validation. https://ajv.js.org/
k
how did you wrap up in suitescript ? @Richard Tanner
how did you download it and added into suitescript
r
its AMD compatible. You can put the js file into your file cabinet and then include it in the define of script.
k
could you please send me link where can I download file ?
r
If you are going to run it serverside, make sure you set the logger option to false. Otherwise it will error trying to use console.log. Use something like this when you initialize it.
Copy code
new Ajv({ logger: false });
You can probably download it from here: https://cdnjs.com/libraries/ajv
k
whats is your define function in script ?
also, whats entry function to use
any sample code
Whenever I define, I'm receiving error as unexpected
Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"missing ; before statement (/SuiteScripts/Modules/ajv.js#2)","stack":[]}
@Richard Tanner hey any help would really appreciate. I have been really trying to achieve this validation but couldn't do it
r
Here is a proof of concept suitelet to get you started. I copied the example code from the AJV website. It assumes the AJV script file is placed in SuiteScripts/Test and called ajv7.js.
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['SuiteScripts/Test/ajv7'],
    
    (Ajv) => {
        /**
         * Defines the Suitelet script trigger point.
         * @param {Object} scriptContext
         * @param {ServerRequest} scriptContext.request - Incoming request
         * @param {ServerResponse} scriptContext.response - Suitelet response
         * @since 2015.2
         */
        const onRequest = (scriptContext) => {
            const ajv=new Ajv({allErrors: true,logger: false});

            const schema = {
                type: "object",
                properties: {
                    foo: {type: "integer"},
                    bar: {type: "string"}
                },
                required: ["foo"],
                additionalProperties: false
            }
            const validate = ajv.compile(schema);

            const data = {
                foo: 1,
                bar: "abc"
            }

            const valid = validate(data);

            scriptContext.response.write(JSON.stringify(valid));

        }

        return {onRequest}

    });
k
is there way you could please share that ajv7.js file ?
ajv.js
is this right file ?
r
yes
k
Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"missing ; before statement (/SuiteScripts/Modules/ajv.js#2)","stack":[]}
@Richard Tanner I'm always getting this error if I use file
@Richard Tanner Thank you for help. I finally get to work it.