Last active
September 10, 2025 10:08
-
-
Save nikku/c97d6cad69fb77aa085a0294999839cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this shows how we can extend AJV@8 to report <deprecated> properties as warnings | |
const Ajv = require('ajv'); | |
const ajv = new Ajv({ | |
allErrors: true | |
}); | |
const schema = { | |
type: 'object', | |
properties: { | |
country: { type: 'string' }, | |
city: { type: 'string', deprecated: true } | |
}, | |
required: ['country'] | |
}; | |
ajv.removeKeyword('deprecated'); | |
ajv.addKeyword({ | |
keyword: 'deprecated', | |
modifying: true, | |
compile(schema, parentSchema, it) { | |
return function (data, dataCtx) { | |
if (schema === true && data !== undefined) { | |
return false; | |
} | |
return true; | |
}; | |
}, | |
errors: false | |
}); | |
const validate = ajv.compile(schema); | |
const data = { | |
country: 'USA', | |
city: 'New York' | |
}; | |
const valid = validate(data); | |
const [ | |
errors, | |
warnings | |
] = (validate.errors || []).reduce( | |
([ errors, warnings ], error) => | |
error.keyword !== 'deprecated' | |
? [ [ ...errors, error ], warnings ] | |
: [ errors, [ ...warnings, error ] ], | |
[ [], [] ] | |
); | |
if (!errors.length) { | |
console.log('Validation succeeded!'); | |
} else { | |
console.log('Validation errors:', errors); | |
} | |
console.log('Validation warnings:', warnings); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment