chore(#197): ran prettier on packages/bruno-schema

This commit is contained in:
Anoop M D 2023-09-22 00:43:56 +05:30
parent f68eacfe0d
commit 88c16fa388
7 changed files with 147 additions and 113 deletions

View File

@ -1,5 +1,5 @@
const Yup = require('yup'); const Yup = require('yup');
const { uidSchema } = require("../common"); const { uidSchema } = require('../common');
const environmentVariablesSchema = Yup.object({ const environmentVariablesSchema = Yup.object({
uid: uidSchema, uid: uidSchema,
@ -7,14 +7,17 @@ const environmentVariablesSchema = Yup.object({
value: Yup.string().nullable(), value: Yup.string().nullable(),
type: Yup.string().oneOf(['text']).required('type is required'), type: Yup.string().oneOf(['text']).required('type is required'),
enabled: Yup.boolean().defined() enabled: Yup.boolean().defined()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const environmentSchema = Yup.object({ const environmentSchema = Yup.object({
uid: uidSchema, uid: uidSchema,
name: Yup.string().min(1).required('name is required'), name: Yup.string().min(1).required('name is required'),
variables: Yup.array().of(environmentVariablesSchema).required('variables are required') variables: Yup.array().of(environmentVariablesSchema).required('variables are required')
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const environmentsSchema = Yup.array().of(environmentSchema); const environmentsSchema = Yup.array().of(environmentSchema);
@ -24,7 +27,9 @@ const keyValueSchema = Yup.object({
value: Yup.string().nullable(), value: Yup.string().nullable(),
description: Yup.string().nullable(), description: Yup.string().nullable(),
enabled: Yup.boolean() enabled: Yup.boolean()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const varsSchema = Yup.object({ const varsSchema = Yup.object({
uid: uidSchema, uid: uidSchema,
@ -33,25 +38,35 @@ const varsSchema = Yup.object({
description: Yup.string().nullable(), description: Yup.string().nullable(),
local: Yup.boolean(), local: Yup.boolean(),
enabled: Yup.boolean() enabled: Yup.boolean()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const requestUrlSchema = Yup.string().min(0).defined(); const requestUrlSchema = Yup.string().min(0).defined();
const requestMethodSchema = Yup.string().oneOf(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']).required('method is required'); const requestMethodSchema = Yup.string()
.oneOf(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])
.required('method is required');
const graphqlBodySchema = Yup.object({ const graphqlBodySchema = Yup.object({
query: Yup.string().nullable(), query: Yup.string().nullable(),
variables: Yup.string().nullable(), variables: Yup.string().nullable()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const requestBodySchema = Yup.object({ const requestBodySchema = Yup.object({
mode: Yup.string().oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql']).required('mode is required'), mode: Yup.string()
.oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql'])
.required('mode is required'),
json: Yup.string().nullable(), json: Yup.string().nullable(),
text: Yup.string().nullable(), text: Yup.string().nullable(),
xml: Yup.string().nullable(), xml: Yup.string().nullable(),
formUrlEncoded: Yup.array().of(keyValueSchema).nullable(), formUrlEncoded: Yup.array().of(keyValueSchema).nullable(),
multipartForm: Yup.array().of(keyValueSchema).nullable(), multipartForm: Yup.array().of(keyValueSchema).nullable(),
graphql: graphqlBodySchema.nullable() graphql: graphqlBodySchema.nullable()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
// Right now, the request schema is very tightly coupled with http request // Right now, the request schema is very tightly coupled with http request
// As we introduce more request types in the future, we will improve the definition to support // As we introduce more request types in the future, we will improve the definition to support
@ -65,22 +80,27 @@ const requestSchema = Yup.object({
script: Yup.object({ script: Yup.object({
req: Yup.string().nullable(), req: Yup.string().nullable(),
res: Yup.string().nullable() res: Yup.string().nullable()
}).noUnknown(true).strict(), })
.noUnknown(true)
.strict(),
vars: Yup.object({ vars: Yup.object({
req: Yup.array().of(varsSchema).nullable(), req: Yup.array().of(varsSchema).nullable(),
res: Yup.array().of(varsSchema).nullable() res: Yup.array().of(varsSchema).nullable()
}).noUnknown(true).strict().nullable(), })
.noUnknown(true)
.strict()
.nullable(),
assertions: Yup.array().of(keyValueSchema).nullable(), assertions: Yup.array().of(keyValueSchema).nullable(),
tests: Yup.string().nullable() tests: Yup.string().nullable()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const itemSchema = Yup.object({ const itemSchema = Yup.object({
uid: uidSchema, uid: uidSchema,
type: Yup.string().oneOf(['http-request', 'graphql-request', 'folder']).required('type is required'), type: Yup.string().oneOf(['http-request', 'graphql-request', 'folder']).required('type is required'),
seq: Yup.number().min(1), seq: Yup.number().min(1),
name: Yup.string() name: Yup.string().min(1, 'name must be atleast 1 characters').required('name is required'),
.min(1, 'name must be atleast 1 characters')
.required('name is required'),
request: requestSchema.when('type', { request: requestSchema.when('type', {
is: (type) => ['http-request', 'graphql-request'].includes(type), is: (type) => ['http-request', 'graphql-request'].includes(type),
then: (schema) => schema.required('request is required when item-type is request') then: (schema) => schema.required('request is required when item-type is request')
@ -88,14 +108,14 @@ const itemSchema = Yup.object({
items: Yup.lazy(() => Yup.array().of(itemSchema)), items: Yup.lazy(() => Yup.array().of(itemSchema)),
filename: Yup.string().nullable(), filename: Yup.string().nullable(),
pathname: Yup.string().nullable() pathname: Yup.string().nullable()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
const collectionSchema = Yup.object({ const collectionSchema = Yup.object({
version: Yup.string().oneOf(['1']).required('version is required'), version: Yup.string().oneOf(['1']).required('version is required'),
uid: uidSchema, uid: uidSchema,
name: Yup.string() name: Yup.string().min(1, 'name must be atleast 1 characters').required('name is required'),
.min(1, 'name must be atleast 1 characters')
.required('name is required'),
items: Yup.array().of(itemSchema), items: Yup.array().of(itemSchema),
activeEnvironmentUid: Yup.string() activeEnvironmentUid: Yup.string()
.length(21, 'activeEnvironmentUid must be 21 characters in length') .length(21, 'activeEnvironmentUid must be 21 characters in length')
@ -108,8 +128,9 @@ const collectionSchema = Yup.object({
items: Yup.array() items: Yup.array()
}), }),
collectionVariables: Yup.object() collectionVariables: Yup.object()
}).noUnknown(true).strict(); })
.noUnknown(true)
.strict();
module.exports = { module.exports = {
requestSchema, requestSchema,

View File

@ -1,6 +1,6 @@
const { expect } = require('@jest/globals'); const { expect } = require('@jest/globals');
const { uuid } = require("../utils/testUtils"); const { uuid } = require('../utils/testUtils');
const { collectionSchema } = require("./index"); const { collectionSchema } = require('./index');
describe('Collection Schema Validation', () => { describe('Collection Schema Validation', () => {
it('collection schema must validate successfully - simple collection, no items', async () => { it('collection schema must validate successfully - simple collection, no items', async () => {
@ -31,11 +31,13 @@ describe('Collection Schema Validation', () => {
version: '1', version: '1',
uid: uuid(), uid: uuid(),
name: 'My Collection', name: 'My Collection',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'A Folder', name: 'A Folder',
type: 'folder' type: 'folder'
}] }
]
}; };
const isValid = await collectionSchema.validate(collection); const isValid = await collectionSchema.validate(collection);
@ -47,7 +49,8 @@ describe('Collection Schema Validation', () => {
version: '1', version: '1',
uid: uuid(), uid: uuid(),
name: 'My Collection', name: 'My Collection',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'Get Countries', name: 'Get Countries',
type: 'http-request', type: 'http-request',
@ -60,7 +63,8 @@ describe('Collection Schema Validation', () => {
mode: 'none' mode: 'none'
} }
} }
}] }
]
}; };
const isValid = await collectionSchema.validate(collection); const isValid = await collectionSchema.validate(collection);
@ -72,16 +76,20 @@ describe('Collection Schema Validation', () => {
version: '1', version: '1',
uid: uuid(), uid: uuid(),
name: 'My Collection', name: 'My Collection',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'First Level Folder', name: 'First Level Folder',
type: 'folder', type: 'folder',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'Second Level Folder', name: 'Second Level Folder',
type: 'folder' type: 'folder'
}] }
}] ]
}
]
}; };
const isValid = await collectionSchema.validate(collection); const isValid = await collectionSchema.validate(collection);
@ -93,11 +101,13 @@ describe('Collection Schema Validation', () => {
version: '1', version: '1',
uid: uuid(), uid: uuid(),
name: 'My Collection', name: 'My Collection',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'First Level Folder', name: 'First Level Folder',
type: 'folder', type: 'folder',
items: [{ items: [
{
uid: uuid(), uid: uuid(),
name: 'Get Countries', name: 'Get Countries',
type: 'http-request', type: 'http-request',
@ -110,12 +120,15 @@ describe('Collection Schema Validation', () => {
mode: 'none' mode: 'none'
} }
} }
}, { },
{
uid: uuid(), uid: uuid(),
name: 'Second Level Folder', name: 'Second Level Folder',
type: 'folder' type: 'folder'
}] }
}] ]
}
]
}; };
const isValid = await collectionSchema.validate(collection); const isValid = await collectionSchema.validate(collection);

View File

@ -1,6 +1,6 @@
const { expect } = require('@jest/globals'); const { expect } = require('@jest/globals');
const { uuid, validationErrorWithMessages } = require("../utils/testUtils"); const { uuid, validationErrorWithMessages } = require('../utils/testUtils');
const { itemSchema } = require("./index"); const { itemSchema } = require('./index');
describe('Item Schema Validation', () => { describe('Item Schema Validation', () => {
it('item schema must validate successfully - simple items', async () => { it('item schema must validate successfully - simple items', async () => {
@ -21,9 +21,7 @@ describe('Item Schema Validation', () => {
}; };
return Promise.all([ return Promise.all([
expect(itemSchema.validate(item)).rejects.toEqual( expect(itemSchema.validate(item)).rejects.toEqual(validationErrorWithMessages('name is required'))
validationErrorWithMessages('name is required')
)
]); ]);
}); });

View File

@ -1,6 +1,6 @@
const { expect } = require('@jest/globals'); const { expect } = require('@jest/globals');
const { uuid, validationErrorWithMessages } = require("../utils/testUtils"); const { uuid, validationErrorWithMessages } = require('../utils/testUtils');
const { requestSchema } = require("./index"); const { requestSchema } = require('./index');
describe('Request Schema Validation', () => { describe('Request Schema Validation', () => {
it('request schema must validate successfully - simple request', async () => { it('request schema must validate successfully - simple request', async () => {
@ -31,7 +31,9 @@ describe('Request Schema Validation', () => {
return Promise.all([ return Promise.all([
expect(requestSchema.validate(request)).rejects.toEqual( expect(requestSchema.validate(request)).rejects.toEqual(
validationErrorWithMessages('method must be one of the following values: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS') validationErrorWithMessages(
'method must be one of the following values: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS'
)
) )
]); ]);
}); });

View File

@ -1,8 +1,8 @@
const { collectionSchema, itemSchema, environmentSchema, environmentsSchema } = require("./collections"); const { collectionSchema, itemSchema, environmentSchema, environmentsSchema } = require('./collections');
module.exports = { module.exports = {
itemSchema, itemSchema,
environmentSchema, environmentSchema,
environmentsSchema, environmentsSchema,
collectionSchema, collectionSchema
}; };

View File

@ -14,7 +14,7 @@ const validationErrorWithMessages = (...errors) => {
return expect.objectContaining({ return expect.objectContaining({
errors errors
}); });
} };
module.exports = { module.exports = {
uuid, uuid,