ajv-validator / ajv-validator/ajv-keywords

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

Open
#257 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
257
Forks
51
PR merge metrics
No merged PRs in 30d

Description

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.
```typescript
const validateData = {
name: 'foo',
};
const dataDefaults = {
'name-string': 'bar',
'details-string': 'baz',
};
```
And I have a schema coming from a different location
```typescript
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:
```typescript
{
name: 'foo',
details: 'baz',
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.