AlvinSeville7cf

JSON validation in Google Script

Aug 30th, 2021 (edited)
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function main() {
  2.   let shema = {
  3.     "$schema": "http://json-schema.org/draft-04/schema#",
  4.     type: "object",
  5.     properties: {
  6.       folders: {
  7.         type: "array",
  8.         items: {
  9.           type: "string",
  10.           minLength: 1
  11.         },
  12.         minItems: 1,
  13.         uniqueItems: true
  14.       },
  15.       actions: {
  16.         type: "array",
  17.         items: {
  18.           type: "object",
  19.           properties: {
  20.             type: {
  21.               type: "string",
  22.               pattern: "^document|spreadsheet|presentation|image|video|script$"
  23.             },
  24.             name: {
  25.               type: "string",
  26.               minLength: 1
  27.             },
  28.             action: {
  29.               oneOf: [
  30.                 {
  31.                   type: "object",
  32.                   properties: {
  33.                     type: {
  34.                       type: "string",
  35.                       pattern: "^delete$"
  36.                     }
  37.                   },
  38.                   additionalProperties: false,
  39.                   required: ["type"]
  40.                 },
  41.                 {
  42.                   type: "object",
  43.                   properties: {
  44.                     type: {
  45.                       type: "string",
  46.                       pattern: "^move$"
  47.                     },
  48.                     target: {
  49.                       type: "string",
  50.                       minLength: 1
  51.                     }
  52.                   },
  53.                   additionalProperties: false,
  54.                   required: ["type", "target"]
  55.                 },
  56.               ]
  57.             }
  58.           },
  59.           additionalProperties: false,
  60.           required: ["name", "action"]
  61.         },
  62.         minItems: 1,
  63.         uniqueItems: true
  64.       },
  65.     },
  66.     additionalProperties: false,
  67.     required: ["actions"]
  68.   }
  69.  
  70.   let json = {
  71.     folders: ["a"],
  72.     actions: [
  73.       {
  74.         type: "document",
  75.         name: "x",
  76.         action: {
  77.           type: "delete",
  78.         }
  79.       }
  80.     ],
  81.   }
  82.  
  83.   let data = {
  84.     schema: shema,
  85.     json: json
  86.   }
  87.  
  88.   var options = {
  89.     method: "post",
  90.     contentType: "application/json",
  91.     muteHttpExceptions: true,
  92.     payload: JSON.stringify(data)
  93.   };
  94.  
  95.   let responce = UrlFetchApp.fetch("https://assertible.com/json", options);
  96.   console.log(`Responce code: ${responce.getResponseCode()}`);
  97.   console.log(`Content: ${responce.getContentText()}`);
  98. }
  99.  
Add Comment
Please, Sign In to add comment