Skip to content

Commit

Permalink
feat: Project Put Schema (#603)
Browse files Browse the repository at this point in the history
* created put schema

* added test still in draft

* added more tests

* commenting out the API, to be worked in a different PR

* linter actin up

* fixed all the repetitive code

* removed unneccesary tests

* Just to trigger the actions again
  • Loading branch information
Ansaar-Syed authored Nov 14, 2023
1 parent c2d208f commit f8fcc19
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 98 deletions.
27 changes: 27 additions & 0 deletions packages/shared/src/schema/project/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod';

export const validation = {
MIN_NAME_LENGTH: 5,
MAX_NAME_LENGTH: 50,
MIN_DESCRIPTION_LENGTH: 20,
MAX_DESCRIPTION_LENGTH: 200,
MAX_LOCATION_LENGTH: 100,
};

export const core = z.object({
name: z.string().trim().min(validation.MIN_NAME_LENGTH).max(validation.MAX_NAME_LENGTH),
description: z
.string()
.trim()
.min(validation.MIN_DESCRIPTION_LENGTH)
.max(validation.MAX_DESCRIPTION_LENGTH),
location: z.string().trim().max(validation.MAX_LOCATION_LENGTH).optional(),
repoUrl: z
.string()
.trim()
.url()
.refine(
(url) => url.startsWith('https://github.com/') || url.startsWith('https://gitlabs.com/'),
'Not a supported repo hosting platform',
),
});
2 changes: 2 additions & 0 deletions packages/shared/src/schema/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './post';
export * from './put';
export * from './core';
export * as contributors from './contributors';
31 changes: 6 additions & 25 deletions packages/shared/src/schema/project/post.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
import { z } from 'zod';
import { core } from './core';

export const PostValidation = {
MIN_NAME_LENGTH: 5,
MAX_NAME_LENGTH: 50,
MIN_DESCRIPTION_LENGTH: 20,
MAX_DESCRIPTION_LENGTH: 200,
MAX_LOCATION_LENGTH: 100,
};

export const post = z.object({
name: z.string().trim().min(PostValidation.MIN_NAME_LENGTH).max(PostValidation.MAX_NAME_LENGTH),
description: z
.string()
.trim()
.min(PostValidation.MIN_DESCRIPTION_LENGTH)
.max(PostValidation.MAX_DESCRIPTION_LENGTH),
location: z.string().trim().max(PostValidation.MAX_LOCATION_LENGTH).optional(),
repoUrl: z
.string()
.trim()
.url()
.refine(
(url) => url.startsWith('https://github.com/') || url.startsWith('https://gitlabs.com/'),
'Not a supported repo hosting platform',
),
});
export const post = core.merge(
z.object({
// Add respective properties.
}),
);
8 changes: 8 additions & 0 deletions packages/shared/src/schema/project/put.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';
import { core } from './core';

export const put = core.merge(
z.object({
// Add respective properties.
}),
);
89 changes: 89 additions & 0 deletions packages/shared/tests/schema/project/core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Schema } from '../../../src';
import { validation } from '../../../src/schema/project';

const validProject = {
name: 'Coders Hub',
description: 'Where coding meets coffee, creating chaos and laughter on line code at a time!',
location: 'cloud 9 3/4',
repoUrl: 'https://github.com/',
};

describe('project put schema', () => {
it('validates matching object', () => {
expect(Schema.project.core.safeParse(validProject).success).toBe(true);
});

it('validates a matching object without a location', () => {
const projectWithoutLocation = { ...validProject } as Record<string, string>;
delete projectWithoutLocation.location;

expect(Schema.project.core.safeParse(projectWithoutLocation).success).toBe(true);
});

it('trims relevant field', () => {
const project = {
...validProject,
location: 'somewhere',
name: 'someone',
description: 'something something dark side',
};
const result = Schema.project.core.safeParse(project);

if (!result.success) throw new Error(result.error.toString());

const { description, name, location } = result.data;
expect(description).toBe(project.description.trim());
expect(name).toBe(project.name.trim());
expect(location).toBe(project.location.trim());
});

it('does not validate an object with an invalid name', () => {
expect(
Schema.project.core.safeParse({
...validProject,
name: Array(validation.MAX_NAME_LENGTH + 1).fill('A'),
}).success,
).toBe(false);

expect(
Schema.project.core.safeParse({
...validProject,
name: Array(validation.MIN_NAME_LENGTH - 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validate an object with an invalid description', () => {
expect(
Schema.project.core.safeParse({
...validProject,
description: Array(validation.MAX_DESCRIPTION_LENGTH + 1).fill('A'),
}).success,
).toBe(false);

expect(
Schema.project.core.safeParse({
...validProject,
description: Array(validation.MIN_DESCRIPTION_LENGTH - 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validate an object with an invalid location', () => {
expect(
Schema.project.core.safeParse({
...validProject,
location: Array(validation.MAX_LOCATION_LENGTH + 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validates an invalid repoUrl', () => {
expect(
Schema.project.core.safeParse({
...validProject,
repoUrl: 'https://google.com',
}).success,
).toBe(false);
});
});
77 changes: 4 additions & 73 deletions packages/shared/tests/schema/project/post.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Schema } from '../../../src';
import { PostValidation } from '../../../src/schema/project';

const validProject = {
name: 'Red Hat Hackers',
description:
'Some super cool project that has a long description that passes validation successfully',
location: 'Outer Space',
name: 'Coders Hub',
description: 'Where coding meets coffee, creating chaos and laughter on line code at a time!!',
location: 'cloud 9 3/4',
repoUrl: 'https://github.com/',
};

describe('project post schema', () => {
it('validates a matching object', () => {
it('validates matching object', () => {
expect(Schema.project.post.safeParse(validProject).success).toBe(true);
});

Expand All @@ -20,71 +18,4 @@ describe('project post schema', () => {

expect(Schema.project.post.safeParse(projectWithoutLocation).success).toBe(true);
});

it('trims relevant fields', () => {
const project = {
...validProject,
location: ' Somewhere ',
name: ' Someone ',
description: ' Something something dark side ',
};
const result = Schema.project.post.safeParse(project);

if (!result.success) throw new Error(result.error.toString());

const { description, name, location } = result.data;
expect(description).toBe(project.description.trim());
expect(name).toBe(project.name.trim());
expect(location).toBe(project.location.trim());
});

it('does not validate an object with an invalid name', () => {
expect(
Schema.project.post.safeParse({
...validProject,
name: Array(PostValidation.MAX_NAME_LENGTH + 1).fill('A'),
}).success,
).toBe(false);

expect(
Schema.project.post.safeParse({
...validProject,
name: Array(PostValidation.MIN_NAME_LENGTH - 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validate an object with an invalid description', () => {
expect(
Schema.project.post.safeParse({
...validProject,
description: Array(PostValidation.MAX_DESCRIPTION_LENGTH + 1).fill('A'),
}).success,
).toBe(false);

expect(
Schema.project.post.safeParse({
...validProject,
description: Array(PostValidation.MIN_DESCRIPTION_LENGTH - 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validate an object with an invalid location', () => {
expect(
Schema.project.post.safeParse({
...validProject,
location: Array(PostValidation.MAX_LOCATION_LENGTH + 1).fill('A'),
}).success,
).toBe(false);
});

it('does not validates an invalid repoUrl', () => {
expect(
Schema.project.post.safeParse({
...validProject,
repoUrl: 'https://google.com',
}).success,
).toBe(false);
});
});
21 changes: 21 additions & 0 deletions packages/shared/tests/schema/project/put.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Schema } from '../../../src';

const validProject = {
name: 'Coders Hub',
description: 'Where coding meets coffee, creating chaos and laughter on line code at a time!',
location: 'cloud 9 3/4',
repoUrl: 'https://github.com/',
};

describe('project put schema', () => {
it('validates matching object', () => {
expect(Schema.project.put.safeParse(validProject).success).toBe(true);
});

it('validates a matching object without a location', () => {
const projectWithoutLocation = { ...validProject } as Record<string, string>;
delete projectWithoutLocation.location;

expect(Schema.project.put.safeParse(projectWithoutLocation).success).toBe(true);
});
});

0 comments on commit f8fcc19

Please sign in to comment.