Skip to content

Commit 6dd5406

Browse files
aditi-khare-mongoDBbaileympearson
authored andcommitted
tests added
1 parent d782f4c commit 6dd5406

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

test/types/schema.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
ValidateOpts,
2525
BufferToBinary
2626
} from 'mongoose';
27-
import { Binary } from 'mongodb';
27+
import { Binary, BSON } from 'mongodb';
2828
import { IsPathRequired } from '../../types/inferschematype';
2929
import { expectType, expectError, expectAssignable } from 'tsd';
3030
import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype';
@@ -591,6 +591,16 @@ const batchSchema2 = new Schema({ name: String }, { discriminatorKey: 'kind', st
591591
} } });
592592
batchSchema2.discriminator('event', eventSchema2);
593593

594+
595+
function encryptionType() {
596+
const keyId = new BSON.UUID();
597+
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'newFakeEncryptionType' }));
598+
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 1 }));
599+
600+
expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'queryableEncryption' }));
601+
expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'csfle' }));
602+
}
603+
594604
function gh11828() {
595605
interface IUser {
596606
name: string;

test/types/schemaTypeOptions.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BSON } from 'mongodb';
12
import {
23
AnyArray,
34
Schema,
@@ -74,3 +75,39 @@ function defaultOptions() {
7475
expectType<Record<string, any>>(new Schema.Types.Subdocument('none').defaultOptions);
7576
expectType<Record<string, any>>(new Schema.Types.UUID('none').defaultOptions);
7677
}
78+
79+
function encrypt() {
80+
const keyId = new BSON.UUID();
81+
82+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' };
83+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' };
84+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' };
85+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Indexed' };
86+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Unindexed' };
87+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Range' };
88+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: undefined };
89+
90+
// qe + valid queries
91+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: 'equality' };
92+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: 'range' };
93+
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: undefined };
94+
95+
// empty object
96+
expectError<SchemaTypeOptions<string>['encrypt']>({});
97+
98+
// invalid keyId
99+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: 'fakeId' });
100+
101+
// missing keyId
102+
expectError<SchemaTypeOptions<string>['encrypt']>({ queries: 'equality' });
103+
expectError<SchemaTypeOptions<string>['encrypt']>({ algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' });
104+
105+
// invalid algorithm
106+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, algorithm: 'SHA_FAKE_ALG' });
107+
108+
// invalid queries
109+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, queries: 'fakeQueryOption' });
110+
111+
// invalid input option
112+
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, invalidKey: 'fakeKeyOption' });
113+
}

types/schemaoptions.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ declare module 'mongoose' {
258258
* @default false
259259
*/
260260
overwriteModels?: boolean;
261+
262+
/**
263+
* Required when the schema is encrypted.
264+
*/
265+
encryptionType?: 'csfle' | 'queryableEncryption';
261266
}
262267

263268
interface DefaultSchemaOptions {

types/schematypes.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BSON } from 'bson';
2+
13
declare module 'mongoose' {
24

35
/** The Mongoose Date [SchemaType](/docs/schematypes.html). */
@@ -207,6 +209,23 @@ declare module 'mongoose' {
207209
maxlength?: number | [number, string] | readonly [number, string];
208210

209211
[other: string]: any;
212+
213+
encrypt?: {
214+
/** The id of the dataKey to use for encryption */
215+
keyId: BSON.UUID;
216+
217+
/**
218+
* Specifies the type of queries that the field can be queried on for Queryable Encryption.
219+
* Required when `SchemaOptions.encryptionType` is 'queryableEncryption'
220+
*/
221+
queries?: 'equality' | 'range';
222+
223+
/**
224+
* The algorithm to use for encryption.
225+
* Required when `SchemaOptions.encryptionType` is 'csfle'
226+
*/
227+
algorithm?: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' | 'Indexed' | 'Unindexed' | 'Range';
228+
};
210229
}
211230

212231
interface Validator<DocType = any> {

0 commit comments

Comments
 (0)