forked from extern/bruno
feat: bruno schema definition for workspace
This commit is contained in:
parent
6bb3967379
commit
7ddfac1ece
@ -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
|
||||
|
||||
```
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
10
packages/bruno-schema/package.json
Normal file
10
packages/bruno-schema/package.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "@usebruno/schema",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"yup": "^0.32.11"
|
||||
}
|
||||
}
|
9
packages/bruno-schema/src/common/index.js
Normal file
9
packages/bruno-schema/src/common/index.js
Normal 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
|
||||
};
|
5
packages/bruno-schema/src/index.js
Normal file
5
packages/bruno-schema/src/index.js
Normal file
@ -0,0 +1,5 @@
|
||||
const { workspaceSchema} = require("./workspaces");
|
||||
|
||||
module.exports = {
|
||||
workspaceSchema
|
||||
};
|
22
packages/bruno-schema/src/utils/testUtils.js
Normal file
22
packages/bruno-schema/src/utils/testUtils.js
Normal 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
|
||||
};
|
15
packages/bruno-schema/src/workspaces/index.js
Normal file
15
packages/bruno-schema/src/workspaces/index.js
Normal 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
|
||||
};
|
72
packages/bruno-schema/src/workspaces/index.spec.js
Normal file
72
packages/bruno-schema/src/workspaces/index.spec.js
Normal 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')
|
||||
)
|
||||
]);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user