feat: bruno schema definition for workspace

This commit is contained in:
Anoop M D 2022-10-14 21:51:59 +05:30
parent 6bb3967379
commit 7ddfac1ece
8 changed files with 154 additions and 2 deletions

View File

@ -1 +1,15 @@
## development
```bash
# install deps
npm i
# run next app
npm run dev --workspace=packages/bruno-app
```
# testing
```bash
# bruno-schema
npm test --workspace=packages/bruno-schema
```

View File

@ -1,10 +1,15 @@
{
"name": "bruno-workspace",
"name": "usebruno",
"private": true,
"workspaces": [
"packages/bruno-app",
"packages/bruno-electron",
"packages/bruno-tauri",
"packages/bruno-schema",
"packages/bruno-testbench"
]
],
"devDependencies": {
"jest": "^29.2.0",
"randomstring": "^1.2.2"
}
}

View File

@ -0,0 +1,10 @@
{
"name": "@usebruno/schema",
"main": "src/index.js",
"scripts": {
"test": "jest"
},
"peerDependencies": {
"yup": "^0.32.11"
}
}

View File

@ -0,0 +1,9 @@
const Yup = require('yup');
const uidSchema = Yup.string()
.length(21, 'uid must be 21 characters in length')
.matches(/^[a-zA-Z0-9]*$/, 'uid must be alphanumeric');
module.exports = {
uidSchema
};

View File

@ -0,0 +1,5 @@
const { workspaceSchema} = require("./workspaces");
module.exports = {
workspaceSchema
};

View File

@ -0,0 +1,22 @@
const { customAlphabet } = require('nanoid');
const { expect } = require('@jest/globals');
// a customized version of nanoid without using _ and -
const uuid = () => {
// https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
const urlAlphabet = 'useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict';
const customNanoId = customAlphabet (urlAlphabet, 21);
return customNanoId();
};
const validationErrorWithMessages = (...errors) => {
return expect.objectContaining({
errors
});
}
module.exports = {
uuid,
validationErrorWithMessages
};

View File

@ -0,0 +1,15 @@
const Yup = require('yup');
const { uidSchema } = require("../common");
const workspaceSchema = Yup.object({
uid: uidSchema,
name: Yup.string()
.min(1, 'name must be atleast 1 characters')
.max(50, 'name must be 50 characters or less')
.required('name is required'),
collectionUids: Yup.array().of(uidSchema)
});
module.exports = {
workspaceSchema
};

View File

@ -0,0 +1,72 @@
const { expect } = require('@jest/globals');
const { uuid, validationErrorWithMessages } = require("../utils/testUtils");
const randomstring = require("randomstring");
const { workspaceSchema } = require("./index");
describe('Workspace Schema Validation', () => {
it('workspace schema must validate successfully', async () => {
const workspace = {
uid: uuid(),
name: 'My workspace'
};
const isValid = await workspaceSchema.validate(workspace);
expect(isValid).toBeTruthy();
});
it('workspace schema must throw error upon invalid uuid length', async () => {
const workspace = {
uid: uuid() + 'junk',
name: 'My workspace'
};
return Promise.all([
expect(workspaceSchema.validate(workspace)).rejects.toEqual(
validationErrorWithMessages('uid must be 21 characters in length')
)
]);
});
it('workspace schema must throw error upon invalid uuid character', async () => {
const workspace = {
uid: uuid(),
name: 'My workspace'
};
workspace.uid = '$' + workspace.uid.substring(1, workspace.uid.length);
return Promise.all([
expect(workspaceSchema.validate(workspace)).rejects.toEqual(
validationErrorWithMessages('uid must be alphanumeric')
)
]);
});
it('workspace schema must throw error when name is empty', async () => {
const workspace = {
uid: uuid()
};
return Promise.all([
expect(workspaceSchema.validate(workspace)).rejects.toEqual(
validationErrorWithMessages('name is required')
)
]);
});
it('workspace schema must throw error when name has more than 50 characters', async () => {
const workspace = {
uid: uuid(),
name: randomstring.generate({
length: 51,
charset: 'alphabetic'
})
};
return Promise.all([
expect(workspaceSchema.validate(workspace)).rejects.toEqual(
validationErrorWithMessages('name must be 50 characters or less')
)
]);
});
});