mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-24 17:03:47 +01:00
chore(#197): ran prettier on packages/bruno-schema
This commit is contained in:
parent
f68eacfe0d
commit
88c16fa388
@ -1,5 +1,5 @@
|
||||
const Yup = require('yup');
|
||||
const { uidSchema } = require("../common");
|
||||
const { uidSchema } = require('../common');
|
||||
|
||||
const environmentVariablesSchema = Yup.object({
|
||||
uid: uidSchema,
|
||||
@ -7,14 +7,17 @@ const environmentVariablesSchema = Yup.object({
|
||||
value: Yup.string().nullable(),
|
||||
type: Yup.string().oneOf(['text']).required('type is required'),
|
||||
enabled: Yup.boolean().defined()
|
||||
}).noUnknown(true).strict();
|
||||
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const environmentSchema = Yup.object({
|
||||
uid: uidSchema,
|
||||
name: Yup.string().min(1).required('name is required'),
|
||||
variables: Yup.array().of(environmentVariablesSchema).required('variables are required')
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const environmentsSchema = Yup.array().of(environmentSchema);
|
||||
|
||||
@ -24,7 +27,9 @@ const keyValueSchema = Yup.object({
|
||||
value: Yup.string().nullable(),
|
||||
description: Yup.string().nullable(),
|
||||
enabled: Yup.boolean()
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const varsSchema = Yup.object({
|
||||
uid: uidSchema,
|
||||
@ -33,25 +38,35 @@ const varsSchema = Yup.object({
|
||||
description: Yup.string().nullable(),
|
||||
local: Yup.boolean(),
|
||||
enabled: Yup.boolean()
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
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({
|
||||
query: Yup.string().nullable(),
|
||||
variables: Yup.string().nullable(),
|
||||
}).noUnknown(true).strict();
|
||||
query: Yup.string().nullable(),
|
||||
variables: Yup.string().nullable()
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const requestBodySchema = Yup.object({
|
||||
mode: Yup.string().oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql']).required('mode is required'),
|
||||
json: Yup.string().nullable(),
|
||||
text: Yup.string().nullable(),
|
||||
xml: Yup.string().nullable(),
|
||||
formUrlEncoded: Yup.array().of(keyValueSchema).nullable(),
|
||||
multipartForm: Yup.array().of(keyValueSchema).nullable(),
|
||||
mode: Yup.string()
|
||||
.oneOf(['none', 'json', 'text', 'xml', 'formUrlEncoded', 'multipartForm', 'graphql'])
|
||||
.required('mode is required'),
|
||||
json: Yup.string().nullable(),
|
||||
text: Yup.string().nullable(),
|
||||
xml: Yup.string().nullable(),
|
||||
formUrlEncoded: Yup.array().of(keyValueSchema).nullable(),
|
||||
multipartForm: Yup.array().of(keyValueSchema).nullable(),
|
||||
graphql: graphqlBodySchema.nullable()
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
// 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
|
||||
@ -65,38 +80,43 @@ const requestSchema = Yup.object({
|
||||
script: Yup.object({
|
||||
req: Yup.string().nullable(),
|
||||
res: Yup.string().nullable()
|
||||
}).noUnknown(true).strict(),
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict(),
|
||||
vars: Yup.object({
|
||||
req: 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(),
|
||||
tests: Yup.string().nullable()
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const itemSchema = Yup.object({
|
||||
uid: uidSchema,
|
||||
type: Yup.string().oneOf(['http-request', 'graphql-request', 'folder']).required('type is required'),
|
||||
seq: Yup.number().min(1),
|
||||
name: Yup.string()
|
||||
.min(1, 'name must be atleast 1 characters')
|
||||
.required('name is required'),
|
||||
name: Yup.string().min(1, 'name must be atleast 1 characters').required('name is required'),
|
||||
request: requestSchema.when('type', {
|
||||
is: (type) => ['http-request', 'graphql-request'].includes(type),
|
||||
then: (schema) => schema.required('request is required when item-type is request')
|
||||
}),
|
||||
is: (type) => ['http-request', 'graphql-request'].includes(type),
|
||||
then: (schema) => schema.required('request is required when item-type is request')
|
||||
}),
|
||||
items: Yup.lazy(() => Yup.array().of(itemSchema)),
|
||||
filename: Yup.string().nullable(),
|
||||
pathname: Yup.string().nullable()
|
||||
}).noUnknown(true).strict();
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
const collectionSchema = Yup.object({
|
||||
version: Yup.string().oneOf(['1']).required('version is required'),
|
||||
uid: uidSchema,
|
||||
name: Yup.string()
|
||||
.min(1, 'name must be atleast 1 characters')
|
||||
.required('name is required'),
|
||||
items: Yup.array().of(itemSchema),
|
||||
name: Yup.string().min(1, 'name must be atleast 1 characters').required('name is required'),
|
||||
items: Yup.array().of(itemSchema),
|
||||
activeEnvironmentUid: Yup.string()
|
||||
.length(21, 'activeEnvironmentUid must be 21 characters in length')
|
||||
.matches(/^[a-zA-Z0-9]*$/, 'uid must be alphanumeric')
|
||||
@ -108,8 +128,9 @@ const collectionSchema = Yup.object({
|
||||
items: Yup.array()
|
||||
}),
|
||||
collectionVariables: Yup.object()
|
||||
}).noUnknown(true).strict();
|
||||
|
||||
})
|
||||
.noUnknown(true)
|
||||
.strict();
|
||||
|
||||
module.exports = {
|
||||
requestSchema,
|
||||
@ -117,4 +138,4 @@ module.exports = {
|
||||
environmentSchema,
|
||||
environmentsSchema,
|
||||
collectionSchema
|
||||
};
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
const { expect } = require('@jest/globals');
|
||||
const { uuid } = require("../utils/testUtils");
|
||||
const { collectionSchema } = require("./index");
|
||||
const { uuid } = require('../utils/testUtils');
|
||||
const { collectionSchema } = require('./index');
|
||||
|
||||
describe('Collection Schema Validation', () => {
|
||||
it('collection schema must validate successfully - simple collection, no items', async () => {
|
||||
@ -31,11 +31,13 @@ describe('Collection Schema Validation', () => {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [{
|
||||
uid: uuid(),
|
||||
name: 'A Folder',
|
||||
type: 'folder'
|
||||
}]
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'A Folder',
|
||||
type: 'folder'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
@ -47,57 +49,8 @@ describe('Collection Schema Validation', () => {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [{
|
||||
uid: uuid(),
|
||||
name: 'Get Countries',
|
||||
type: 'http-request',
|
||||
request: {
|
||||
url: 'https://restcountries.com/v2/alpha/in',
|
||||
method: 'GET',
|
||||
headers: [],
|
||||
params: [],
|
||||
body: {
|
||||
mode: 'none'
|
||||
}
|
||||
}
|
||||
}]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('collection schema must validate successfully - simple collection, folder inside folder', async () => {
|
||||
const collection = {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [{
|
||||
uid: uuid(),
|
||||
name: 'First Level Folder',
|
||||
type: 'folder',
|
||||
items: [{
|
||||
uid: uuid(),
|
||||
name: 'Second Level Folder',
|
||||
type: 'folder'
|
||||
}]
|
||||
}]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('collection schema must validate successfully - simple collection, [folder] [request + folder]', async () => {
|
||||
const collection = {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [{
|
||||
uid: uuid(),
|
||||
name: 'First Level Folder',
|
||||
type: 'folder',
|
||||
items: [{
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'Get Countries',
|
||||
type: 'http-request',
|
||||
@ -110,12 +63,72 @@ describe('Collection Schema Validation', () => {
|
||||
mode: 'none'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('collection schema must validate successfully - simple collection, folder inside folder', async () => {
|
||||
const collection = {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'Second Level Folder',
|
||||
type: 'folder'
|
||||
}]
|
||||
}]
|
||||
name: 'First Level Folder',
|
||||
type: 'folder',
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'Second Level Folder',
|
||||
type: 'folder'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('collection schema must validate successfully - simple collection, [folder] [request + folder]', async () => {
|
||||
const collection = {
|
||||
version: '1',
|
||||
uid: uuid(),
|
||||
name: 'My Collection',
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'First Level Folder',
|
||||
type: 'folder',
|
||||
items: [
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'Get Countries',
|
||||
type: 'http-request',
|
||||
request: {
|
||||
url: 'https://restcountries.com/v2/alpha/in',
|
||||
method: 'GET',
|
||||
headers: [],
|
||||
params: [],
|
||||
body: {
|
||||
mode: 'none'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
uid: uuid(),
|
||||
name: 'Second Level Folder',
|
||||
type: 'folder'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const isValid = await collectionSchema.validate(collection);
|
||||
|
@ -1,6 +1,6 @@
|
||||
const { expect } = require('@jest/globals');
|
||||
const { uuid, validationErrorWithMessages } = require("../utils/testUtils");
|
||||
const { itemSchema } = require("./index");
|
||||
const { uuid, validationErrorWithMessages } = require('../utils/testUtils');
|
||||
const { itemSchema } = require('./index');
|
||||
|
||||
describe('Item Schema Validation', () => {
|
||||
it('item schema must validate successfully - simple items', async () => {
|
||||
@ -21,9 +21,7 @@ describe('Item Schema Validation', () => {
|
||||
};
|
||||
|
||||
return Promise.all([
|
||||
expect(itemSchema.validate(item)).rejects.toEqual(
|
||||
validationErrorWithMessages('name is required')
|
||||
)
|
||||
expect(itemSchema.validate(item)).rejects.toEqual(validationErrorWithMessages('name is required'))
|
||||
]);
|
||||
});
|
||||
|
||||
@ -68,4 +66,4 @@ describe('Item Schema Validation', () => {
|
||||
)
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
const { expect } = require('@jest/globals');
|
||||
const { uuid, validationErrorWithMessages } = require("../utils/testUtils");
|
||||
const { requestSchema } = require("./index");
|
||||
const { uuid, validationErrorWithMessages } = require('../utils/testUtils');
|
||||
const { requestSchema } = require('./index');
|
||||
|
||||
describe('Request Schema Validation', () => {
|
||||
it('request schema must validate successfully - simple request', async () => {
|
||||
@ -31,8 +31,10 @@ describe('Request Schema Validation', () => {
|
||||
|
||||
return Promise.all([
|
||||
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'
|
||||
)
|
||||
)
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -8,4 +8,4 @@ const uidSchema = Yup.string()
|
||||
|
||||
module.exports = {
|
||||
uidSchema
|
||||
};
|
||||
};
|
||||
|
@ -1,8 +1,8 @@
|
||||
const { collectionSchema, itemSchema, environmentSchema, environmentsSchema } = require("./collections");
|
||||
const { collectionSchema, itemSchema, environmentSchema, environmentsSchema } = require('./collections');
|
||||
|
||||
module.exports = {
|
||||
itemSchema,
|
||||
environmentSchema,
|
||||
environmentsSchema,
|
||||
collectionSchema,
|
||||
};
|
||||
collectionSchema
|
||||
};
|
||||
|
@ -5,7 +5,7 @@ const { expect } = require('@jest/globals');
|
||||
const uuid = () => {
|
||||
// https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
|
||||
const urlAlphabet = 'useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict';
|
||||
const customNanoId = customAlphabet (urlAlphabet, 21);
|
||||
const customNanoId = customAlphabet(urlAlphabet, 21);
|
||||
|
||||
return customNanoId();
|
||||
};
|
||||
@ -14,7 +14,7 @@ const validationErrorWithMessages = (...errors) => {
|
||||
return expect.objectContaining({
|
||||
errors
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
uuid,
|
||||
|
Loading…
Reference in New Issue
Block a user