Skip to content

Commit

Permalink
fix(validation): fix issue where object field validation were written…
Browse files Browse the repository at this point in the history
… on individual fields, causing unintended validation rule leakage
  • Loading branch information
bjoerge committed Jun 23, 2021
1 parent efe8315 commit 934bdf6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
9 changes: 0 additions & 9 deletions packages/@sanity/validation/src/inferFromSchemaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,7 @@ function inferForFields(typeDef, schema, visited) {
return
}

const fieldRules = typeDef.validation
.map((rule) => rule._fieldRules)
.filter(Boolean)
.reduce((acc, current) => ({fields: {...acc.fields, ...current}, hasRules: true}), {
fields: {},
hasRules: false,
})

typeDef.fields.forEach((field) => {
field.type.validation = fieldRules.fields[field.name] || field.type.validation
inferFromSchemaType(field.type, schema, visited)
})
}
Expand Down
34 changes: 31 additions & 3 deletions packages/@sanity/validation/src/validateDocument.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Type = require('type-of-is')
const {flatten} = require('lodash')
const ValidationError = require('./ValidationError')
const Rule = require('./Rule')

/* eslint-disable no-console */
module.exports = async (doc, schema) => {
Expand Down Expand Up @@ -54,14 +55,41 @@ function validateObject(obj, type, path, options) {

// Validate fields within object
const fields = type.fields || []

const fieldRules = type.validation
.map((rule) => rule._fieldRules)
.filter(Boolean)
.reduce(Object.assign, {})

const fieldChecks = fields.map((field) => {
const validation = field.type && field.type.validation
if (!validation) {
// field validation from the enclosing object type

const fieldValidation = fieldRules[field.name]
if (!fieldValidation) {
return []
}
const fieldPath = appendPath(path, field.name)
const fieldValue = obj[field.name]

return fieldValidation(new Rule())
.validate(fieldValue, {
parent: obj,
document: options.document,
path: fieldPath,
type: field.type,
})
.then((result) => applyPath(result, fieldPath))
})

const fieldTypeChecks = fields.map((field) => {
// field validation from field type

const fieldPath = appendPath(path, field.name)
const fieldValue = obj[field.name]
const validation = field.type && field.type.validation
if (!validation) {
return []
}
return validateItem(fieldValue, field.type, fieldPath, {
parent: obj,
document: options.document,
Expand All @@ -70,7 +98,7 @@ function validateObject(obj, type, path, options) {
})
})

return Promise.all([...objChecks, ...fieldChecks]).then(flatten)
return Promise.all([...objChecks, ...fieldChecks, ...fieldTypeChecks]).then(flatten)
}

function validateArray(items, type, path, options) {
Expand Down

2 comments on commit 934bdf6

@vercel
Copy link

@vercel vercel bot commented on 934bdf6 Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 934bdf6 Jun 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

Please sign in to comment.