fix(validator): fix subsequent nested objects names (#683)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(validator): fix subsequent nested objects names fix the bug of having subsequent objects inside a nested structure causing the validator to forget the previous keys in the scheme's path fixes point 1 of #677 * deonify changes
authored by Dario Piotrowicz · committed by GitHub · parent e9a54bb · Browse files · 3 files·+95 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+95 added−42 removed
- tests:+93 added−28 removed
- other:+2 added−14 removed
Provenance
- Commit
07efdb5- Tree
038da00- Author
Dario Piotrowicz <dario.piotrowicz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
e9a54bb
3 files changed+95 added−42 removed
modified:deno_dist/validator/validator.ts+1 added-7 removed
44 unmodified lines45 }46 47 getValidators = (): VBase[] => {48 const validators: VBase[] = []49- const thisKeys: string[] = []50- Object.assign(thisKeys, this.keys)49 50 const walk = (container: T, keys: string[], isOptional: boolean) => {51 for (const v of Object.values(container)) {52 if (v instanceof VArray || v instanceof VObject) {53 // eslint-disable-next-line @typescript-eslint/ban-ts-comment54 // @ts-ignore55 isOptional ||= v._isOptional58- keys.push(...v.keys)59- walk(v.container as T, keys, isOptional)56+ walk(v.container as T, [...keys, ...v.keys], isOptional)60- const tmp: string[] = []61- Object.assign(tmp, thisKeys)62- keys = tmp57 } else if (v instanceof VBase) {58 if (isOptional) v.isOptional()59 v.baseKeys.push(...keys)60 validators.push(v)437 unmodified linesmodified:src/validator/validator.test.ts+93 added-28 removed
729 unmodified lines730 expect(results[0].message).toBe(`${messages[i]}`)731 }732 })733 734- it('Should threat `v.json()` as a nested array type only inside an array', async () => {735- const json = {736- blog: {737- totalPosts: '3',738- posts: [739- {740- id: 123,741- title: 'JavaScript',742- author: {743- name: 'foo',744- },734+ const blogJson = {735+ blog: {736+ totalPosts: '3',737+ meta: {738+ foo: {739+ bar: 'test',740+ }741+ },742+ posts: [743+ {744+ id: 123,745+ title: 'JavaScript',746+ author: {747+ name: 'foo',748 },746- {747- id: '456',748- title: 'Framework',749- author: {750- name: 'bar',751- },749+ publisher: {750+ name: 'Foo',751 },753- {754- id: 789,755- title: 'Hono',756- author: {757- name: 'baz',758- },752+ },753+ {754+ id: '456',755+ title: 'Framework',756+ author: {757+ name: 'bar',758 },760- ],761- highlightedPostId: '123',762- }759+ publisher: {760+ name: 'Bar',761+ },762+ },763+ {764+ id: 789,765+ title: 'Hono',766+ author: {767+ name: 'baz',768+ },769+ publisher: {770+ name: 'Baz',771+ },772+ },773+ ],774+ highlightedPostId: '123',775 }776+ }764- 777+778+ it('Should threat `v.json()` as a nested array type only inside an array', async () => {779 const req = new Request('http://localhost/', {780 method: 'POST',767- body: JSON.stringify(json),781+ body: JSON.stringify(blogJson),782 })783 784 const v = new Validator()785 const vObject = v.object('blog', v => ({27 unmodified lines813 const highlightedPostResult = await validators[4].validate(req)814 expect(highlightedPostResult[0].isValid).toBe(false)815 expect(highlightedPostResult[0].message).toBe('Invalid Value [123]: the JSON body "blog.highlightedPostId" is invalid - should be "number"')816 })817+818+ it('Should correctly handle sequences of nested objects inside an array', async () => {819+ const req = new Request('http://localhost/', {820+ method: 'POST',821+ body: JSON.stringify(blogJson),822+ })823+824+ const v = new Validator()825+ const vObject = v.object('blog', v => ({826+ posts: v.array('posts', v => ({827+ author: v.object('author', v => ({828+ name: v.json('name'),829+ })),830+ publisher: v.object('publisher', v => ({831+ name: v.json('name'),832+ }))833+ })),834+ }))835+836+ for (const validator of vObject.getValidators()) {837+ const results = await validator.validate(req)838+ expect(results[0].isValid).toBe(true)839+ expect(results[0].message).toBeUndefined()840+ }841+ })842+843+ it('Should correctly handle values inside objects and arrays', async () => {844+ const req = new Request('http://localhost/', {845+ method: 'POST',846+ body: JSON.stringify(blogJson),847+ })848+849+ const v = new Validator()850+ const vObject = v.object('blog', (v) => ({851+ meta: v.object('meta', (v) => ({852+ foo: v.object('foo', (v) => ({853+ bar: v.json('bar'),854+ })),855+ })),856+ posts: v.array('posts', (v) => ({857+ author: v.object('author', (v) => ({858+ name: v.json('name'),859+ })),860+ })),861+ }))862+ for (const validator of vObject.getValidators()) {863+ const results = await validator.validate(req)864+ expect(results[0].isValid).toBe(true)865+ expect(results[0].message).toBeUndefined()866+ }867+ })868 })869 870 describe('Custom message', () => {871 const v = new Validator()121 unmodified linesmodified:src/validator/validator.ts+1 added-7 removed
44 unmodified lines45 }46 47 getValidators = (): VBase[] => {48 const validators: VBase[] = []49- const thisKeys: string[] = []50- Object.assign(thisKeys, this.keys)49 50 const walk = (container: T, keys: string[], isOptional: boolean) => {51 for (const v of Object.values(container)) {52 if (v instanceof VArray || v instanceof VObject) {53 // eslint-disable-next-line @typescript-eslint/ban-ts-comment54 // @ts-ignore55 isOptional ||= v._isOptional58- keys.push(...v.keys)59- walk(v.container as T, keys, isOptional)56+ walk(v.container as T, [...keys, ...v.keys], isOptional)60- const tmp: string[] = []61- Object.assign(tmp, thisKeys)62- keys = tmp57 } else if (v instanceof VBase) {58 if (isOptional) v.isOptional()59 v.baseKeys.push(...keys)60 validators.push(v)437 unmodified lines