Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add pattern check to the name and the description of the items #371

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Item/Item.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,59 @@ describe('Item router', () => {
itemToUpsert = utils.omit(dbItem, ['created_at', 'updated_at'])
})

describe('and the item inserted has an invalid name', () => {
it("should fail with a message indicating that the name doesn't match the pattern", () => {
return server
.put(buildURL(url))
.send({ item: { ...itemToUpsert, name: 'anInvalid:name' } })
.set(createAuthHeaders('put', url))
.expect(STATUS_CODES.badRequest)
.then((response: any) => {
expect(response.body).toEqual({
data: [
{
dataPath: '/item/name',
keyword: 'pattern',
message: 'should match pattern "^[^:]*$"',
params: { pattern: '^[^:]*$' },
schemaPath: '#/properties/item/properties/name/pattern',
},
],
error: 'Invalid request body',
ok: false,
})
})
})
})

describe('and the item inserted has an invalid description', () => {
it("should fail with a message indicating that the description doesn't match the pattern", () => {
return server
.put(buildURL(url))
.send({
item: { ...itemToUpsert, description: 'anInvalid:nescription' },
})
.set(createAuthHeaders('put', url))
.expect(STATUS_CODES.badRequest)
.then((response: any) => {
expect(response.body).toEqual({
data: [
{
dataPath: '/item/description',
keyword: 'pattern',
message: 'should match pattern "^[^:]*$"',
params: { pattern: '^[^:]*$' },
schemaPath:
'#/properties/item/properties/description/pattern',
},
],
error: 'Invalid request body',
ok: false,
})
})
})
})

describe('and the param id is different from payload id', () => {
it('should fail with body and url ids do not match message', async () => {
const response = await server
Expand Down
8 changes: 6 additions & 2 deletions src/Item/Item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ export const itemSchema = Object.freeze({
properties: {
id: { type: 'string', format: 'uuid' },
urn: { type: ['string', 'null'] },
name: { type: 'string', maxLength: 32 },
description: { type: ['string', 'null'], maxLength: 64 },
name: { type: 'string', maxLength: 32, pattern: '^[^:]*$' },
description: {
type: ['string', 'null'],
maxLength: 64,
pattern: '^[^:]*$',
Comment on lines +11 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these have a + instead of an * ? or at least a minLength key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only check that the builder has is this one, there's sadly no check about the length. We could add one, but we'll be potentially introducing a breaking change.

},
thumbnail: { type: 'string' },
eth_address: { type: 'string' },
collection_id: { type: ['string', 'null'], format: 'uuid' },
Expand Down