Skip to content

feathers dove: default is ignored for #2776

Closed

Description

@prajwal497

Steps to reproduce

Create a Schema with a nested object property for any class.
Give default: (any value) inside properties of that nested object (type) like below:

export const courseDataSchema = schema({
    $id: 'CourseData',
    type: 'object',
    additionalProperties: false,
    required: ['account', 'has_certificate'], //
    properties: {
      title: { type: 'string' },
      nested_obj: {
        name: { type: 'string' },
        status: { type: 'string', default: 'active'},
      }
  }
});

Expected behavior

This should work without any error.

Actual behavior

Error: strict mode: default is ignored for: data16.status

Feathersjs version: "^5.0.0-pre.29"

NodeJS version: "16.17.0"

Operating System:

Browser Version:

React Native Version:

Module Loader:

Activity

  1. daffl commented on Oct 6, 2022

    @daffl
    Member

    It appears this is the default AJV behaviour - to enable this, the useDefaults Ajv option needs to be set. See https://ajv.js.org/options.html#usedefaults and https://dove.feathersjs.com/api/schema/schema.html#ajv

  2. prajwal497 commented on Oct 7, 2022

    @prajwal497
    Author

    It is already enabled and it does work on a simple structured schema with no sub or nested objects,
    but does not work for sub documents.

  3. daffl commented on Oct 7, 2022

    @daffl
    Member

    My guess is that it is because your nested object definition should look like this:

    export const courseDataSchema = schema({
      $id: "CourseData",
      type: "object",
      additionalProperties: false,
      required: ["account", "has_certificate"], //
      properties: {
        title: { type: "string" },
        nested_obj: {
          type: "object",
          properties: {
            name: { type: "string" },
            status: { type: "string", default: "active" },
          }
        },
      },
    });
  4. prajwal497 commented on Oct 10, 2022

    @prajwal497
    Author

    Whats the diffference?

  5. daffl commented on Oct 11, 2022

    @daffl
    Member

    Yours is invalid JSON schema. An object definition (including nested ones) always needs to have a

    nested_obj: {
      type: "object",
      properties: {
      }
    }
  6. prajwal497 commented on Oct 12, 2022

    @prajwal497
    Author

    Yes i may have missed that in the example but my code does have types for all sub documents (objects and arrays):
    Updated Example:

    export const courseDataSchema = schema({
        $id: 'CourseData',
        type: 'object',
        additionalProperties: false,
        required: [], //
        properties: {
          title: { type: 'string' },
          nested_obj: {
            type: 'object',
            additionalProperties: false,
            properties:{
               name: { type: 'string' },
               status: { type: 'string', default: 'active'}
            }
          }
      }
    });
    

    or

    export const courseDataSchema = schema({
      $id: 'CourseData',
      type: 'object',
      additionalProperties: false,
      required: [], //
      properties: {
        title: { type: 'string' },
        nested_array: {
          type: 'array',
          items: {
            type: 'object',
            additionalProperties: false,
            properties: {
              name: { type: 'string' },
              status: { type: 'string', default: 'active' }
            }
          }
        }
      }
    });
    
  7. daffl commented on Oct 12, 2022

    @daffl
    Member

    This appears to still be a limitation of AJV. As noted in ajv-validator/ajv#1710 (comment) if the top level object does not exist the nested schema (and its defaults) will never be evaluated. One thing to try would be setting an empty object in a resolver before the validator runs:

    courseDataResolver = resolve({
      properties: {
        nested_obj: async value => (value || {})
      }
    })
  8. prajwal497 commented on Oct 13, 2022

    @prajwal497
    Author

    I will test this and get back to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions