Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
anirudhmungre opened this issue Feb 28, 2022 · 0 comments

Comments

@anirudhmungre
Copy link

anirudhmungre commented Feb 28, 2022

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',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant