Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type SchemaValidationErrorType =
| 'forbidden'
| 'const'
| 'enum'
| 'additionalProperties'
/**
* Schema composition keywords (allOf, anyOf, oneOf, not)
* These keywords apply subschemas in a logical manner according to JSON Schema spec
Expand Down
2 changes: 2 additions & 0 deletions src/errors/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export function getErrorMessage(
throw new Error('"minContains" is not implemented yet')
case 'maxContains':
throw new Error('"maxContains" is not implemented yet')
case 'additionalProperties':
return 'Additional property is not allowed'
case 'json-logic':
return customErrorMessage || 'The value is not valid'
}
Expand Down
27 changes: 27 additions & 0 deletions src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ function validateJsonLogicSchema(value: SchemaValue, schema: JsfSchema | undefin
return validateSchema(value, schema, options, path, jsonLogicContext)
}

interface CompiledPattern { regex: RegExp }

function compilePatternProperties(patternProperties: Record<string, any> = {}): CompiledPattern[] {
return Object.keys(patternProperties).map(
pattern => ({ regex: new RegExp(pattern) }),
)
}

/**
* Validate a value against a schema
* @param value - The value to validate
Expand Down Expand Up @@ -259,6 +267,25 @@ export function validateSchema(
}
}

if (schema.additionalProperties === false && isObjectValue(value)) {
const definedProps = new Set(Object.keys(schema.properties || {}))
const compiledPatterns = compilePatternProperties(schema.patternProperties)

for (const key of Object.keys(value)) {
const isDefined = definedProps.has(key)
const matchesPattern = compiledPatterns.some(({ regex }) => regex.test(key))

if (!isDefined && !matchesPattern) {
errors.push({
path: [...path, key],
validation: 'additionalProperties',
schema,
value: value[key],
})
}
}
}

return [
...errors,
// JSON-schema spec validations
Expand Down
93 changes: 93 additions & 0 deletions test/validation/additional_properties.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import type { JsfObjectSchema } from '../../src/types'
import { describe, expect, it } from '@jest/globals'
import { createHeadlessForm } from '../../src'

describe('additionalProperties validation', () => {
describe('basic additionalProperties: false', () => {
const schema: JsfObjectSchema = {
type: 'object',
properties: {
a: { type: 'integer' },
b: { type: 'string' },
},
additionalProperties: false,
}
const form = createHeadlessForm(schema)

it('allows objects with only defined properties', () => {
expect(form.handleValidation({ a: 1 })).not.toHaveProperty('formErrors')
expect(form.handleValidation({ a: 1, b: 'test' })).not.toHaveProperty('formErrors')
})

it('rejects objects with additional properties', () => {
expect(form.handleValidation({ a: 1, c: 'extra' })).toMatchObject({
formErrors: { c: 'Additional property is not allowed' },
})

expect(form.handleValidation({ a: 1, b: 'test', c: 'extra' })).toMatchObject({
formErrors: { c: 'Additional property is not allowed' },
})
})

it('rejects objects with only additional properties', () => {
expect(form.handleValidation({ c: 'extra' })).toMatchObject({
formErrors: { c: 'Additional property is not allowed' },
})
})
})

describe('additionalProperties: false with patternProperties', () => {
const schema: JsfObjectSchema = {
type: 'object',
properties: {
foo: {},
bar: {},
},
patternProperties: {
'^v': {},
},
additionalProperties: false,
}
const form = createHeadlessForm(schema)

it('allows properties defined in properties', () => {
expect(form.handleValidation({ foo: 1 })).not.toHaveProperty('formErrors')
expect(form.handleValidation({ foo: 1, bar: 2 })).not.toHaveProperty('formErrors')
})

it('allows properties matching patternProperties', () => {
expect(form.handleValidation({ vroom: 1 })).not.toHaveProperty('formErrors')
expect(form.handleValidation({ vampire: 1 })).not.toHaveProperty('formErrors')
expect(form.handleValidation({ v: 1 })).not.toHaveProperty('formErrors')
})

it('allows combination of properties and patternProperties', () => {
expect(form.handleValidation({ foo: 1, vroom: 2 })).not.toHaveProperty('formErrors')
expect(form.handleValidation({ foo: 1, bar: 2, vampire: 3 })).not.toHaveProperty('formErrors')
})

it('rejects properties that match neither properties nor patternProperties', () => {
expect(form.handleValidation({ foo: 1, quux: 'boom' })).toMatchObject({
formErrors: { quux: 'Additional property is not allowed' },
})

expect(form.handleValidation({ hello: 'world' })).toMatchObject({
formErrors: { hello: 'Additional property is not allowed' },
})
})
})

describe('when additionalProperties is not false', () => {
const schema: JsfObjectSchema = {
type: 'object',
properties: {
a: { type: 'integer' },
},
}
const form = createHeadlessForm(schema)

it('allows additional properties', () => {
expect(form.handleValidation({ a: 1, b: 2, c: 3 })).not.toHaveProperty('formErrors')
})
})
})