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 maxProperties object sampler support #158

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions src/samplers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,31 @@ export function sampleObject(schema, options = {}, spec, context) {
res[`${String(propertyName)}1`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
res[`${String(propertyName)}2`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
}

// Strictly enforce maxProperties constraint
if (schema && typeof schema.properties === 'object' && schema.maxProperties !== undefined && Object.keys(res).length > schema.maxProperties) {
let filteredResult = {};
adamaltman marked this conversation as resolved.
Show resolved Hide resolved
let propertiesAdded = 0;

// Always include required properties first, if present
const requiredProperties = (Array.isArray(schema.required) ? schema.required : []);
requiredProperties.forEach(propName => {
if (res[propName] !== undefined) {
filteredResult[propName] = res[propName];
propertiesAdded++;
}
});

// Add other properties until maxProperties is reached
Object.keys(res).forEach(propName => {
if (propertiesAdded < schema.maxProperties && !filteredResult.hasOwnProperty(propName)) {
filteredResult[propName] = res[propName];
propertiesAdded++;
}
});

res = filteredResult;
}

return res;
}
25 changes: 24 additions & 1 deletion test/unit/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,28 @@ describe('sampleObject', () => {
fooId: 'fb4274c7-4fcd-4035-8958-a680548957ff',
barId: '3c966637-4898-4972-9a9d-baefa6cd6c89'
});
})
});

it('respects maxProperties by including no more than 2 properties in the sampled object', () => {
// Define a schema with four properties and a maxProperties limit of two
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string', format: 'email' },
age: { type: 'integer', minimum: 0 },
phone: { type: 'string' }
},
required: ['name', 'email'], // 'name' and 'email' are required
maxProperties: 2
};

const result = sampleObject(schema);

expect(Object.keys(result).length).to.be.at.most(2);

// Assert that if 'name' and 'email' are required, they are included
expect(result).to.have.property('name');
expect(result).to.have.property('email');
});
});