Skip to content

Commit

Permalink
Merge pull request #533 from onhate/main
Browse files Browse the repository at this point in the history
fix: log error on missing required fields when finding with fields selection
  • Loading branch information
mobsense authored Jul 3, 2024
2 parents 8fff64f + 16741d6 commit 169fe18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,12 @@ export class Model {
}
} else if (value === undefined) {
if (field.required) {
/*
Transactions transform the properties to return something, but
does not have all the properties and required fields may be missing)
/*
Transactions transform the properties to return something, but
does not have all the properties and required fields may be missing).
Also find operation with fields selections may not include required fields.
*/
if (!params.transaction) {
if (!params.transaction && !params.fields) {
this.table.log.error(`Required field "${name}" in model "${this.name}" not defined in table item`, {
model: this.name, raw, params, field,
})
Expand Down Expand Up @@ -1607,7 +1608,7 @@ export class Model {
delete properties[name]
if (this.getPartial(field, params) === false && pathname.match(/[[.]/)) {
/*
Partial disabled for a nested object
Partial disabled for a nested object
Don't create remove entry as the entire object is being created/updated
*/
continue
Expand Down Expand Up @@ -2034,7 +2035,7 @@ export class Model {

/*
Return if a field supports partial updates of its children.
Only relevant for fields with nested schema
Only relevant for fields with nested schema
*/
getPartial(field, params) {
let partial = params.partial
Expand Down
19 changes: 18 additions & 1 deletion test/find.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/*
find-and-scan.ts - Basic find and scan options
*/
import {AWS, Client, Match, Table, print, dump, delay} from './utils/init'
import {DefaultSchema} from './schemas'
import {Client, Table} from './utils/init'

// jest.setTimeout(7200 * 1000)

const onErrorLog = jest.fn();
const table = new Table({
name: 'FindTable',
client: Client,
partial: false,
schema: DefaultSchema,
logger: (level, message, ctx) => {
if (level === 'info') return console.log({message, ctx})
if (level === 'warn') return console.warn({message, ctx})
if (level === 'error') {
onErrorLog({message, ctx})
return console.error({message, ctx})
}
},
})

test('Create Table', async () => {
Expand All @@ -21,6 +30,7 @@ test('Create Table', async () => {
})

let User = table.getModel('User')
let Pet = table.getModel('Pet')
let user: any
let users: any

Expand Down Expand Up @@ -99,6 +109,13 @@ test('List with begins_with', async () => {
expect(items.length).toBe(1)
})

test('Find Pets names only (should not log error on missing required fields)', async () => {
await Pet.create({name: 'Guinness', race: 'dog', breed:'shih-tzu'})
let items = await Pet.find({}, {fields: ['name']})
expect(items).toHaveLength(1)
expect(onErrorLog).not.toHaveBeenCalled();
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
8 changes: 8 additions & 0 deletions test/schemas/defaultSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export default {
gs3pk: {type: String, value: '${_type}#${status}'},
gs3sk: {type: String, value: '${_type}#${name}'},
},
Pet: {
pk: {type: String, value: '${_type}', hidden: true},
sk: {type: String, value: '${_type}#${id}', hidden: true},
id: {type: String, generate: 'ulid'},
name: {type: String},
race: {type: String, enum: ['dog', 'cat', 'fish'], required: true},
breed: {type: String, required: true},
}
},
params: {
isoDates: true,
Expand Down

0 comments on commit 169fe18

Please sign in to comment.