-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
c2d208f
commit f8fcc19
Showing
7 changed files
with
157 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |