Skip to content

Commit

Permalink
Add dedicated test for field requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
ulazdins-afs authored and DScheglov committed Oct 12, 2024
1 parent 673a03c commit 1b5f252
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
13 changes: 2 additions & 11 deletions test/suites/required-issue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ describe('schema.jsonSchema', () => {
required: true,
unique: true,
},
year: {
type: Number,
required: true,
},
description: {
type: String,
},
internalName: {
type: String,
required() {
return this.year > 2000;
},
required: true,
unique: true,
},
manage: {
Expand Down Expand Up @@ -50,9 +44,6 @@ describe('schema.jsonSchema', () => {
name: {
type: 'string',
},
year: {
type: 'number',
},
description: {
type: 'string',
},
Expand Down Expand Up @@ -81,7 +72,7 @@ describe('schema.jsonSchema', () => {
},
required: [
'name',
'year',
'internalName',
],
});
});
Expand Down
65 changes: 65 additions & 0 deletions test/suites/required.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const mongoose = require('../../index')(require('mongoose'));

const { Schema } = mongoose;
const assert = require('assert');

describe('Required fields: schema.jsonSchema', () => {
it('should correctly translate field requirement', () => {
const bookSchema = new Schema({
name: {
type: String,
required() {
return this.year > 2000;
},
},
year: {
type: Number,
required: true,
},
description: {
type: String,
},
internalName: {
type: String,
required: [true, 'Internal name is required'],
},
author: {
type: String,
required: [
function () {

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@5

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@6

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@7

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@8

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@5

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@6

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@7

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@8

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@5

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@6

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@7

Unexpected unnamed function

Check warning on line 29 in test/suites/required.test.js

View workflow job for this annotation

GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@8

Unexpected unnamed function
return this.year > 2000;
},
'Internal name is required',
],
},
}, { _id: false });

const jsonSchema = bookSchema.jsonSchema('book');

assert.deepEqual(jsonSchema, {
title: 'book',
type: 'object',
properties: {
name: {
type: 'string',
},
year: {
type: 'number',
},
description: {
type: 'string',
},
internalName: {
type: 'string',
},
author: {
type: 'string',
},
},
required: [
'year',
'internalName',
],
});
});
});

0 comments on commit 1b5f252

Please sign in to comment.