-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@5
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@6
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@7
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 16 on ubuntu-latest with Mongoose@8
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@5
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@6
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@7
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 18 on ubuntu-latest with Mongoose@8
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@5
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@6
Check warning on line 29 in test/suites/required.test.js GitHub Actions / Test on Node 20 on ubuntu-latest with Mongoose@7
|
||
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', | ||
], | ||
}); | ||
}); | ||
}); |