From 1a9770b62a4a3585d7c79a5994bc2c405fff33bd Mon Sep 17 00:00:00 2001 From: Ola Date: Wed, 14 Sep 2022 12:21:20 +0200 Subject: [PATCH 1/4] Validate configured plugin options the Gatsby way --- plugin/gatsby-node.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugin/gatsby-node.js b/plugin/gatsby-node.js index 404f160..e10a805 100644 --- a/plugin/gatsby-node.js +++ b/plugin/gatsby-node.js @@ -3,6 +3,23 @@ const { newCloudinary, getResourceOptions } = require('./utils'); const REPORTER_PREFIX = `gatsby-source-cloudinary`; const NODE_TYPE = `CloudinaryMedia`; +// Validation of configured plugin options the Gatsby way + +exports.pluginOptionsSchema = ({ Joi }) => { + return Joi.object({ + cloudName: Joi.string().required(), + apiKey: Joi.string().required(), + apiSecret: Joi.string().required(), + resourceType: Joi.string().default('image'), + resourceType: Joi.string().default('image'), + type: Joi.string().default('all'), + maxResults: Joi.number().integer().positive().default(10), + tags: Joi.boolean().default(false), + prefix: Joi.string(), + context: Joi.boolean(), + }); +}; + const getNodeData = (gatsbyUtils, media, cloudName) => { const { createNodeId, createContentDigest } = gatsbyUtils; From 589acdbfff635a3a294341c8bf3e75fb49c96866 Mon Sep 17 00:00:00 2001 From: Ola Date: Sat, 17 Sep 2022 10:23:53 +0200 Subject: [PATCH 2/4] resultsPerPage --- plugin/gatsby-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/gatsby-node.js b/plugin/gatsby-node.js index e10a805..df596de 100644 --- a/plugin/gatsby-node.js +++ b/plugin/gatsby-node.js @@ -10,8 +10,8 @@ exports.pluginOptionsSchema = ({ Joi }) => { cloudName: Joi.string().required(), apiKey: Joi.string().required(), apiSecret: Joi.string().required(), - resourceType: Joi.string().default('image'), - resourceType: Joi.string().default('image'), + resourceType: Joi.number().integer().positive().default(10), + resultsPerPage: Joi.string().default('image'), type: Joi.string().default('all'), maxResults: Joi.number().integer().positive().default(10), tags: Joi.boolean().default(false), From 1d0d0f27be3ef2eec17eb7a029252f9788fea4a7 Mon Sep 17 00:00:00 2001 From: Ola Date: Tue, 27 Sep 2022 09:37:32 +0200 Subject: [PATCH 3/4] default(Joi.ref('maxResults')), how do we test it? --- plugin/gatsby-node.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugin/gatsby-node.js b/plugin/gatsby-node.js index df596de..0b569f7 100644 --- a/plugin/gatsby-node.js +++ b/plugin/gatsby-node.js @@ -10,8 +10,11 @@ exports.pluginOptionsSchema = ({ Joi }) => { cloudName: Joi.string().required(), apiKey: Joi.string().required(), apiSecret: Joi.string().required(), - resourceType: Joi.number().integer().positive().default(10), - resultsPerPage: Joi.string().default('image'), + resourceType: Joi.string().default('image'), + resultsPerPage: Joi.number() + .integer() + .positive() + .default(Joi.ref('maxResults')), type: Joi.string().default('all'), maxResults: Joi.number().integer().positive().default(10), tags: Joi.boolean().default(false), From a58e352e302207310ff2253aa8e5b06803077542 Mon Sep 17 00:00:00 2001 From: Benedicte Raae Date: Tue, 27 Sep 2022 13:45:54 +0200 Subject: [PATCH 4/4] add test --- plugin/gatsby-node.js | 6 ++-- plugin/gatsby-node.test.js | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 plugin/gatsby-node.test.js diff --git a/plugin/gatsby-node.js b/plugin/gatsby-node.js index 0b569f7..6072d73 100644 --- a/plugin/gatsby-node.js +++ b/plugin/gatsby-node.js @@ -3,20 +3,18 @@ const { newCloudinary, getResourceOptions } = require('./utils'); const REPORTER_PREFIX = `gatsby-source-cloudinary`; const NODE_TYPE = `CloudinaryMedia`; -// Validation of configured plugin options the Gatsby way - exports.pluginOptionsSchema = ({ Joi }) => { return Joi.object({ cloudName: Joi.string().required(), apiKey: Joi.string().required(), apiSecret: Joi.string().required(), resourceType: Joi.string().default('image'), + type: Joi.string().default('all'), + maxResults: Joi.number().integer().positive().default(10), resultsPerPage: Joi.number() .integer() .positive() .default(Joi.ref('maxResults')), - type: Joi.string().default('all'), - maxResults: Joi.number().integer().positive().default(10), tags: Joi.boolean().default(false), prefix: Joi.string(), context: Joi.boolean(), diff --git a/plugin/gatsby-node.test.js b/plugin/gatsby-node.test.js new file mode 100644 index 0000000..7ee1ad7 --- /dev/null +++ b/plugin/gatsby-node.test.js @@ -0,0 +1,72 @@ +import Joi from 'joi'; +import { testPluginOptionsSchema } from 'gatsby-plugin-utils'; +import { pluginOptionsSchema } from './gatsby-node'; + +describe('pluginOptionsSchema', () => { + test('should validate minimal correct options', async () => { + const options = { + cloudName: 'cloudName', + apiKey: 'apiKey', + apiSecret: 'apiSecret', + }; + + const { isValid } = await testPluginOptionsSchema( + pluginOptionsSchema, + options, + ); + + expect(isValid).toBe(true); + }); + + test('should invalidate incorrect options', async () => { + const options = { + cloudName: 120, + apiKey: '', + resourceType: '', + type: 30, + maxResults: '', + resultsPerPage: 'hello', + tags: 'world', + prefix: 800, + context: '', + }; + + const { isValid, errors } = await testPluginOptionsSchema( + pluginOptionsSchema, + options, + ); + + expect(isValid).toBe(false); + expect(errors).toEqual([ + `"cloudName" must be a string`, + `"apiKey" is not allowed to be empty`, + `"apiSecret" is required`, + `"resourceType" is not allowed to be empty`, + `"type" must be a string`, + `"maxResults" must be a number`, + `"resultsPerPage" must be a number`, + `"tags" must be a boolean`, + `"prefix" must be a string`, + `"context" must be a boolean`, + ]); + }); + + test('should add defaults', async () => { + const schema = pluginOptionsSchema({ Joi }); + const options = { + cloudName: 'cloudName', + apiKey: 'apiKey', + apiSecret: 'apiSecret', + }; + const { value } = schema.validate(options); + + expect(value).toEqual({ + ...options, + resourceType: 'image', + maxResults: 10, + resultsPerPage: 10, + tags: false, + type: 'all', + }); + }); +});