Skip to content

I want to dynamically update data at validation time, or modify the schema at compile to include a default on a property #257

@anirudhmungre

Description

@anirudhmungre

Hi :)
I have been given an object that I want to validate as well as an object with defaults for those values if they don't exist.

const validateData = {
  name: 'foo', 
};
const dataDefaults = {
  'name-string': 'bar',
  'details-string': 'baz',
};

And I have a schema coming from a different location

const schema = {
  title: 'Files',
  type: 'object',
  properties: {
    name: {
      type: string,
      description: 'A name',
      title: 'Name',
      'other-name': 'name-string'
    },
    details: {
      type: string,
      description: 'A description of details',
      title: 'Details',
      'other-name': 'details-string'      
    },
  },
  required: ['name', 'details'],
};

Now my object will be invalid because details is required but doesn't exist. However, it does exist in dataDefaults. Is there a way that if during validation I don't find a value for a given property (example above details) that it looks for that property in dataDefaults instead? The schema comes with a custom keyword other-name defining what that property would be called within the dataDefaults object.

If I can modify the schema to add a default param on each property containing an other-name keyword and set that default value to the other name value at compile of the schema then this is solved.

I would expect my final object after validation to look like:

{
  name: 'foo',
  details: 'baz',
}

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