Repository: sid88in/serverless-appsync-plugin
Pull request #512 (2022-06-15 12:32:14)

fix(validation): Caching validation


Patch:
diff --git a/src/validation.ts b/src/validation.ts
index 256cec0..45b2cd7 100644
--- a/src/validation.ts
+++ b/src/validation.ts
@@ -354,7 +354,7 @@ export const appSyncSchema = {
         {
           type: 'object',
           properties: {
-            ttl: { type: 'integer' },
+            ttl: { type: 'integer', minimum: 1, maximum: 3600 },
             keys: {
               type: 'array',
               items: { type: 'string' },
@@ -681,7 +681,9 @@ export const appSyncSchema = {
         enabled: { type: 'boolean' },
         behavior: {
           type: 'string',
-          enum: ['FULL_REQUEST_CACHING' || 'PER_RESOLVER_CACHING'],
+          enum: ['FULL_REQUEST_CACHING', 'PER_RESOLVER_CACHING'],
+          errorMessage:
+            "must be one of 'FULL_REQUEST_CACHING', 'PER_RESOLVER_CACHING'",
         },
         type: {
           enum: [
@@ -694,13 +696,14 @@ export const appSyncSchema = {
             'LARGE_8X',
             'LARGE_12X',
           ],
+          errorMessage:
+            "must be one of 'SMALL', 'MEDIUM', 'LARGE', 'XLARGE', 'LARGE_2X', 'LARGE_4X', 'LARGE_8X', 'LARGE_12X'",
         },
-        ttl: { type: 'number' },
+        ttl: { type: 'integer', minimum: 1, maximum: 3600 },
         atRestEncryption: { type: 'boolean' },
         transitEncryption: { type: 'boolean' },
       },
       required: ['behavior'],
-      errorMessage: 'must be a valid caching config',
     },
     additionalAuthentications: {
       type: 'array',


Test patch:
diff --git a/src/__tests__/validation/__snapshots__/base.test.ts.snap b/src/__tests__/validation/__snapshots__/base.test.ts.snap
index 2d1bc27..5b2fc81 100644
--- a/src/__tests__/validation/__snapshots__/base.test.ts.snap
+++ b/src/__tests__/validation/__snapshots__/base.test.ts.snap
@@ -1,5 +1,18 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`Valdiation Caching Invalid should validate a Invalid 1`] = `
+"/caching/enabled: must be boolean
+/caching/behavior: must be one of 'FULL_REQUEST_CACHING', 'PER_RESOLVER_CACHING'
+/caching/type: must be one of 'SMALL', 'MEDIUM', 'LARGE', 'XLARGE', 'LARGE_2X', 'LARGE_4X', 'LARGE_8X', 'LARGE_12X'
+/caching/ttl: must be integer
+/caching/atRestEncryption: must be boolean
+/caching/transitEncryption: must be boolean"
+`;
+
+exports[`Valdiation Caching Invalid should validate a Ttl max value 1`] = `"/caching/ttl: must be <= 3600"`;
+
+exports[`Valdiation Caching Invalid should validate a Ttl min value 1`] = `"/caching/ttl: must be >= 1"`;
+
 exports[`Valdiation Domain Invalid should validate a Invalid 1`] = `
 "/domain/enabled: must be boolean
 /domain/name: must be a valid domain name
diff --git a/src/__tests__/validation/base.test.ts b/src/__tests__/validation/base.test.ts
index 7c5ee45..ebe5cd5 100644
--- a/src/__tests__/validation/base.test.ts
+++ b/src/__tests__/validation/base.test.ts
@@ -303,4 +303,87 @@ describe('Valdiation', () => {
       });
     });
   });
+
+  describe('Caching', () => {
+    describe('Valid', () => {
+      const assertions = [
+        {
+          name: 'Minimum',
+          config: {
+            ...basicConfig,
+            caching: {
+              behavior: 'PER_RESOLVER_CACHING',
+            },
+          } as AppSyncConfigInput,
+        },
+        {
+          name: 'Full',
+          config: {
+            ...basicConfig,
+            caching: {
+              enabled: true,
+              behavior: 'PER_RESOLVER_CACHING',
+              type: 'SMALL',
+              ttl: 3600,
+              atRestEncryption: true,
+              transitEncryption: true,
+            },
+          } as AppSyncConfigInput,
+        },
+      ];
+
+      assertions.forEach((config) => {
+        it(`should validate a ${config.name}`, () => {
+          expect(validateConfig(config.config)).toBe(true);
+        });
+      });
+    });
+
+    describe('Invalid', () => {
+      const assertions = [
+        {
+          name: 'Invalid',
+          config: {
+            ...basicConfig,
+            caching: {
+              enabled: 'foo',
+              behavior: 'bar',
+              type: 'INVALID',
+              ttl: 'bizz',
+              atRestEncryption: 'bizz',
+              transitEncryption: 'bazz',
+            },
+          },
+        },
+        {
+          name: 'Ttl min value',
+          config: {
+            ...basicConfig,
+            caching: {
+              behavior: 'PER_RESOLVER_CACHING',
+              ttl: 0,
+            },
+          },
+        },
+        {
+          name: 'Ttl max value',
+          config: {
+            ...basicConfig,
+            caching: {
+              behavior: 'PER_RESOLVER_CACHING',
+              ttl: 3601,
+            },
+          },
+        },
+      ];
+
+      assertions.forEach((config) => {
+        it(`should validate a ${config.name}`, () => {
+          expect(function () {
+            validateConfig(config.config);
+          }).toThrowErrorMatchingSnapshot();
+        });
+      });
+    });
+  });
 });