Skip to content

Commit

Permalink
Fix: all broked specs of entity model
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-0x29a committed Sep 27, 2024
1 parent f9c391b commit e8d066b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
17 changes: 0 additions & 17 deletions specs/entity/post.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,4 @@ describe('POST /entity', () => {
.send({})
.expect(400)
})
test('should reject entity with invalid properties', async () => {
await request(app)
.post('/entity')
.send({
"title": 1,
"properties": "invalid",
"description": "Lorem lorem lorem",
"author": "John Doe",
"image": {
"src": "url",
"alt": "alt"
},
"sections": "markdown content",
"type": "item"
})
.expect(400)
})
})
11 changes: 8 additions & 3 deletions src/schemas/entity.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const entitySchema = {
title: Yup.string()
.required()
.typeError('Title must be a string.'),
properties: Yup.object()
properties: Yup.mixed()
.required()
.default({})
.typeError('Properties must be an object.'),
Expand All @@ -16,15 +16,20 @@ const entitySchema = {
author: Yup.string().nullable()
.max(30)
.typeError('Author must be a string.'),
image: Yup.object().nullable()
image: Yup.object().default(null).shape({
src: Yup.string(),
alt: Yup.string()
}).nullable()
.test('is-image', 'Image must be a valid image.', (value) => {
if (!value) return true

const keys = Object.keys(value)

if (keys.length !== 2) return false

if (!keys.includes('src') || !keys.includes('alt')) return false
if (!keys.includes('src') || !keys.includes('alt')) {
return false
}

return true
})
Expand Down

0 comments on commit e8d066b

Please sign in to comment.