Skip to content

Commit

Permalink
fix: Update the depth validation used when writing documents (#1815)
Browse files Browse the repository at this point in the history
fix: Update the depth validation used when writing documents, so that it matches the validation of the Firestore backend.
  • Loading branch information
MarkDuckworth committed Jan 25, 2023
1 parent 653a8e9 commit 789d9eb
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export function validateUserInput(
level?: number,
inArray?: boolean
): void {
if (path && path.size > MAX_DEPTH) {
if (path && path.size - 1 > MAX_DEPTH) {
throw new Error(
`${invalidArgumentMessage(
arg,
Expand Down
246 changes: 246 additions & 0 deletions dev/test/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {it} from 'mocha';
import {expect} from 'chai';
import {validateUserInput} from '../src/serializer';

describe('validateUserInput', () => {
it('validates the depth of nested objects and arrays - 20', () => {
// This nested object is 20 levels deep
const nestedObject = {
// depth 0
links: [
// depth 1
{
// depth 2
child: {
// depth 3
links: [
// depth 4
{
// depth 5
child: {
// depth 6
links: [
// depth 7
{
// depth 8
child: {
// depth 9
links: [
// depth 10
{
// depth 11
child: {
// depth 12
links: [
// depth 13
{
// depth 14
child: {
// depth 15
links: [
// depth 16
{
// depth 17
child: {
// depth 18
uiData: {
// depth 19
choicesFactors: {
// depth 20
rarely: 1,
},
},
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
};

validateUserInput('nestedObject', nestedObject, 'Firestore Object', {
allowDeletes: 'none',
allowTransforms: false,
allowUndefined: false,
});
});

it('validates the depth of nested objects and arrays - 21', () => {
// This nested object is 21 levels deep
const nestedObject = {
// depth 0
links: [
// depth 1
{
// depth 2
child: {
// depth 3
links: [
// depth 4
{
// depth 5
child: {
// depth 6
links: [
// depth 7
{
// depth 8
child: {
// depth 9
links: [
// depth 10
{
// depth 11
child: {
// depth 12
links: [
// depth 13
{
// depth 14
child: {
// depth 15
links: [
// depth 16
{
// depth 17
child: {
// depth 18
uiData: {
// depth 19
choicesFactors: {
// depth 20
rarely: {
// depth 21
cat: true,
},
},
},
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
},
},
],
};

expect(() =>
validateUserInput('nestedObject', nestedObject, 'Firestore Object', {
allowDeletes: 'none',
allowTransforms: false,
allowUndefined: false,
})
).to.throw(/Input object is deeper than 20 levels/i);
});

it('validates the depth of nested objects - 20', () => {
// This nested object is 20 levels deep
const nestedObject = {
a: {
b: {
c: {
d: {
e: {
f: {
g: {
h: {
i: {
j: {
k: {
l: {m: {n: {o: {p: {q: {r: {s: {t: {u: 1}}}}}}}}},
},
},
},
},
},
},
},
},
},
},
},
};

validateUserInput('nestedObject', nestedObject, 'Firestore Object', {
allowDeletes: 'none',
allowTransforms: false,
allowUndefined: false,
});
});

it('validates the depth of nested objects and arrays - 21', () => {
// This nested object is 21 levels deep
const nestedObject = {
a: {
b: {
c: {
d: {
e: {
f: {
g: {
h: {
i: {
j: {
k: {
l: {
m: {n: {o: {p: {q: {r: {s: {t: {u: {v: 1}}}}}}}}},
},
},
},
},
},
},
},
},
},
},
},
},
};

expect(() =>
validateUserInput('nestedObject', nestedObject, 'Firestore Object', {
allowDeletes: 'none',
allowTransforms: false,
allowUndefined: false,
})
).to.throw(/Input object is deeper than 20 levels/i);
});
});

0 comments on commit 789d9eb

Please sign in to comment.