Skip to content

Commit 983ca42

Browse files
committed
fix: ensure subschema errors are not included for undefined obj
fixes #475
1 parent 8e9b4bc commit 983ca42

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/validation/validateField.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
146146
affectedKeyGeneric: affectedKeyGeneric ?? undefined,
147147
keysToValidate
148148
})) {
149-
// Perform validation for this key
149+
// Perform validation for this key
150150
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
151151
def = currentDef
152152

@@ -171,7 +171,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
171171
// Loop through each of the definitions in the SimpleSchemaGroup.
172172
// If the value matches any, we are valid and can stop checking the rest.
173173
for (const [typeIndex, typeDef] of currentDef.type.entries()) {
174-
// If the type is SimpleSchema.Any, then it is valid
174+
// If the type is SimpleSchema.Any, then it is valid
175175
if (typeDef === SimpleSchema.Any) break
176176

177177
const nonAnyTypeDefinition = typeDef as SchemaKeyDefinitionWithOneType
@@ -225,7 +225,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
225225
}
226226
}
227227

228-
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
228+
if (val !== undefined && SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
229229
const itemErrors = validateField({
230230
extendedCustomContext,
231231
keysToValidate,
@@ -242,8 +242,8 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
242242

243243
// As soon as we find a type for which the value is valid, stop checking more
244244
if (fieldValidationErrorsForThisType.length === 0) {
245-
// One we have chosen a valid schema, there is no need to validate the
246-
// properties of this object because we validated all the way down
245+
// One we have chosen a valid schema, there is no need to validate the
246+
// properties of this object because we validated all the way down
247247
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
248248
return fieldValidationErrors
249249
}
@@ -261,7 +261,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
261261

262262
// Mark invalid if not found in schema
263263
if (def == null) {
264-
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
264+
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
265265
if (
266266
op === '$unset' ||
267267
(op === '$currentDate' && affectedKey.endsWith('.$type'))

test/SimpleSchema_required.tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ describe('SimpleSchema - required', function () {
136136
enemies: [{}]
137137
}).toBe(2)
138138
})
139+
140+
it('optional object subschema', function () {
141+
const addressSchema = new SimpleSchema({
142+
city: String,
143+
street: String
144+
})
145+
const personSchema = new SimpleSchema({
146+
name: String,
147+
address: { type: addressSchema, optional: true }
148+
})
149+
150+
expectRequiredErrorLength(personSchema, {
151+
name: 'Bob'
152+
}).toBe(0)
153+
})
139154
})
140155

141156
describe('requiredByDefault', function () {

0 commit comments

Comments
 (0)