Skip to content

Commit

Permalink
fix: ensure subschema errors are not included for undefined obj
Browse files Browse the repository at this point in the history
fixes #475
  • Loading branch information
aldeed committed Feb 26, 2024
1 parent 8e9b4bc commit 983ca42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/validation/validateField.ts
Expand Up @@ -146,7 +146,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
affectedKeyGeneric: affectedKeyGeneric ?? undefined,
keysToValidate
})) {
// Perform validation for this key
// Perform validation for this key
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
def = currentDef

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

const nonAnyTypeDefinition = typeDef as SchemaKeyDefinitionWithOneType
Expand Down Expand Up @@ -225,7 +225,7 @@ export default function validateField (props: ValidateFieldProps): ValidationErr
}
}

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

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

// Mark invalid if not found in schema
if (def == null) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (
op === '$unset' ||
(op === '$currentDate' && affectedKey.endsWith('.$type'))
Expand Down
15 changes: 15 additions & 0 deletions test/SimpleSchema_required.tests.ts
Expand Up @@ -136,6 +136,21 @@ describe('SimpleSchema - required', function () {
enemies: [{}]
}).toBe(2)
})

it('optional object subschema', function () {
const addressSchema = new SimpleSchema({
city: String,
street: String
})
const personSchema = new SimpleSchema({
name: String,
address: { type: addressSchema, optional: true }
})

expectRequiredErrorLength(personSchema, {
name: 'Bob'
}).toBe(0)
})
})

describe('requiredByDefault', function () {
Expand Down

0 comments on commit 983ca42

Please sign in to comment.