Skip to content

Commit

Permalink
[dsch] removing minIteams on the required arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
DScheglov committed Oct 12, 2024
1 parent e6ad49f commit ebecfd7
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ newSchemaType.prototype.jsonSchema = function() {
- version 2.1.0 - Support for <[email protected]> and Node v14.x, v16.x, v18.x
- version 2.2.0 - Support for <[email protected]> and Node v20.x. Node v14.x is no longer supported (use v2.1.0 of the lib)
- version 2.2.1 - fix for `required` fields: if `required` is a function, it is not considered as required field
- version 3.0.0 - breaking changes on Array with `required`: the `minItems` constraint is removed from JSON schema

## Supported versions

Expand Down
4 changes: 0 additions & 4 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ function array_jsonSchema(name) {
result.items = this.caster.jsonSchema(itemName);
}

if (result.items.__required || this.schemaOptions && this.schemaOptions.required) {
result.minItems = 1;
}

__processOptions(result, extendOptions(this));
delete result.items.__required;

Expand Down
3 changes: 1 addition & 2 deletions test/suites/ajv-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ describe('Validation: schema.jsonSchema()', () => {
a: {
type: 'array',
items: { type: 'number' },
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand All @@ -227,7 +226,7 @@ describe('Validation: schema.jsonSchema()', () => {
assert.ok(isValid({ a: [0, 1] }));
assert.ok(isValid({ a: [0] }));
assert.ok(isValid({ }));
assert.ok(!isValid({ a: [] }));
assert.ok(isValid({ a: [] }));
assert.ok(!isValid({ a: [0, 1, 'a'] }));
});

Expand Down
1 change: 0 additions & 1 deletion test/suites/nullable-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('nullable: schema.jsonSchema', () => {
...(isV6pl ? { _id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' } } : undefined),
x: {
type: 'array',
minItems: 1,
items: {
type: 'object',
title: 'itemOf_x',
Expand Down
4 changes: 0 additions & 4 deletions test/suites/population.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Population: Model.jsonSchema()', () => {
'x-ref': 'Person',
description: 'Refers to Person',
},
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand Down Expand Up @@ -108,7 +107,6 @@ describe('Population: Model.jsonSchema()', () => {
'x-ref': 'Person',
description: 'Refers to Person',
},
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand Down Expand Up @@ -176,7 +174,6 @@ describe('Population: Model.jsonSchema()', () => {
'x-ref': 'Person',
description: 'Refers to Person',
},
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand All @@ -203,7 +200,6 @@ describe('Population: Model.jsonSchema()', () => {
description: 'Refers to Person',
pattern: '^[0-9a-fA-F]{24}$',
},
minItems: 1,
},
comment: {
type: 'array',
Expand Down
3 changes: 0 additions & 3 deletions test/suites/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('Queries: query.jsonSchema()', () => {

pattern: '^[0-9a-fA-F]{24}$',
},
minItems: 1,
},
comment: {
type: 'array',
Expand Down Expand Up @@ -87,7 +86,6 @@ describe('Queries: query.jsonSchema()', () => {

pattern: '^[0-9a-fA-F]{24}$',
},
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand Down Expand Up @@ -124,7 +122,6 @@ describe('Queries: query.jsonSchema()', () => {
'x-ref': 'Person',
description: 'Refers to Person',
},
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand Down
1 change: 0 additions & 1 deletion test/suites/readonly.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ describe('readonly: model.jsonSchema', () => {
description: 'Refers to Person',
pattern: '^[0-9a-fA-F]{24}$',
},
minItems: 1,
},
comment: {
type: 'array',
Expand Down
1 change: 0 additions & 1 deletion test/suites/translation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ describe('schema.jsonSchema', () => {
...(isV6pl ? { _id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' } } : undefined),
x: {
type: 'array',
minItems: 1,
items: {
type: 'object',
title: 'itemOf_x',
Expand Down
5 changes: 2 additions & 3 deletions test/suites/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('Validation: schema.jsonSchema()', () => {
assert.equal(errors.length, 0);
});

it('should build schema and validate arrays with minItems constraint', () => {
it('should build schema and validate arrays with**out** minItems constraint', () => {
const mSchema = mongoose.Schema({
a: [{
type: Number,
Expand All @@ -238,7 +238,6 @@ describe('Validation: schema.jsonSchema()', () => {
a: {
type: 'array',
items: { type: 'number' },
minItems: 1,
},
_id: { type: 'string', pattern: '^[0-9a-fA-F]{24}$' },
},
Expand All @@ -255,7 +254,7 @@ describe('Validation: schema.jsonSchema()', () => {
assert.equal(errors.length, 0);

errors = validate({ a: [] }, jsonSchema).errors;
assert.equal(errors.length, 1);
assert.equal(errors.length, 0);

errors = validate({ a: [0, 1, 'a'] }, jsonSchema).errors;
assert.equal(errors.length, 1);
Expand Down

0 comments on commit ebecfd7

Please sign in to comment.