|
| 1 | +import type { JsfObjectSchema } from '../../src/types' |
| 2 | +import { describe, expect, it } from '@jest/globals' |
| 3 | +import { createHeadlessForm } from '../../src' |
| 4 | + |
| 5 | +describe('additionalProperties validation', () => { |
| 6 | + describe('basic additionalProperties: false', () => { |
| 7 | + const schema: JsfObjectSchema = { |
| 8 | + type: 'object', |
| 9 | + properties: { |
| 10 | + a: { type: 'integer' }, |
| 11 | + b: { type: 'string' }, |
| 12 | + }, |
| 13 | + additionalProperties: false, |
| 14 | + } |
| 15 | + const form = createHeadlessForm(schema) |
| 16 | + |
| 17 | + it('allows objects with only defined properties', () => { |
| 18 | + expect(form.handleValidation({ a: 1 })).not.toHaveProperty('formErrors') |
| 19 | + expect(form.handleValidation({ a: 1, b: 'test' })).not.toHaveProperty('formErrors') |
| 20 | + }) |
| 21 | + |
| 22 | + it('rejects objects with additional properties', () => { |
| 23 | + expect(form.handleValidation({ a: 1, c: 'extra' })).toMatchObject({ |
| 24 | + formErrors: { c: 'Additional property is not allowed' }, |
| 25 | + }) |
| 26 | + |
| 27 | + expect(form.handleValidation({ a: 1, b: 'test', c: 'extra' })).toMatchObject({ |
| 28 | + formErrors: { c: 'Additional property is not allowed' }, |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + it('rejects objects with only additional properties', () => { |
| 33 | + expect(form.handleValidation({ c: 'extra' })).toMatchObject({ |
| 34 | + formErrors: { c: 'Additional property is not allowed' }, |
| 35 | + }) |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + describe('additionalProperties: false with patternProperties', () => { |
| 40 | + const schema: JsfObjectSchema = { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + foo: {}, |
| 44 | + bar: {}, |
| 45 | + }, |
| 46 | + patternProperties: { |
| 47 | + '^v': {}, |
| 48 | + }, |
| 49 | + additionalProperties: false, |
| 50 | + } |
| 51 | + const form = createHeadlessForm(schema) |
| 52 | + |
| 53 | + it('allows properties defined in properties', () => { |
| 54 | + expect(form.handleValidation({ foo: 1 })).not.toHaveProperty('formErrors') |
| 55 | + expect(form.handleValidation({ foo: 1, bar: 2 })).not.toHaveProperty('formErrors') |
| 56 | + }) |
| 57 | + |
| 58 | + it('allows properties matching patternProperties', () => { |
| 59 | + expect(form.handleValidation({ vroom: 1 })).not.toHaveProperty('formErrors') |
| 60 | + expect(form.handleValidation({ vampire: 1 })).not.toHaveProperty('formErrors') |
| 61 | + expect(form.handleValidation({ v: 1 })).not.toHaveProperty('formErrors') |
| 62 | + }) |
| 63 | + |
| 64 | + it('allows combination of properties and patternProperties', () => { |
| 65 | + expect(form.handleValidation({ foo: 1, vroom: 2 })).not.toHaveProperty('formErrors') |
| 66 | + expect(form.handleValidation({ foo: 1, bar: 2, vampire: 3 })).not.toHaveProperty('formErrors') |
| 67 | + }) |
| 68 | + |
| 69 | + it('rejects properties that match neither properties nor patternProperties', () => { |
| 70 | + expect(form.handleValidation({ foo: 1, quux: 'boom' })).toMatchObject({ |
| 71 | + formErrors: { quux: 'Additional property is not allowed' }, |
| 72 | + }) |
| 73 | + |
| 74 | + expect(form.handleValidation({ hello: 'world' })).toMatchObject({ |
| 75 | + formErrors: { hello: 'Additional property is not allowed' }, |
| 76 | + }) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('when additionalProperties is not false', () => { |
| 81 | + const schema: JsfObjectSchema = { |
| 82 | + type: 'object', |
| 83 | + properties: { |
| 84 | + a: { type: 'integer' }, |
| 85 | + }, |
| 86 | + // additionalProperties defaults to true/undefined |
| 87 | + } |
| 88 | + const form = createHeadlessForm(schema) |
| 89 | + |
| 90 | + it('allows additional properties', () => { |
| 91 | + expect(form.handleValidation({ a: 1, b: 2, c: 3 })).not.toHaveProperty('formErrors') |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments