Using AJV with JSON validation for hide or replace properties

Viewed 9

I have a concrete scenario. I use JSONSchema for validate .json config files passed to my software.

I need to log (elastic logs, irrelevant for the question) some of theese configuracions (hidding some key properties, passwords, tokens, etc...)

My approach is trying with a custom keywords for my existent schemas.

// One example of my multiple exsist schemas and subschemas i use
const smtpConfigShchema = {
  type: "object",
  properties: {
    url: {type: "string"},
    user: {type: "string"},
    pass: {type: "string", /*my custom keyword ->*/hidden: true},
  }
}

I can create 2 Ajv instances, one just for validates the config (ignoring my custom keywords), and another for validates the config for the logs (using the custom keyword).

const ajvConfigLogger = new Ajv();
ajv.addKeyword({
  keyword: "password",
  code: undefined, // this...,
  validate: undefined,// or this...
  compile: undefined // or this...
})
ajv.validate(smtpConfigShchema, smtpConfigObject)

// The output must be the *hidden* fields in objects empty or replaced ('*****')
console.log(smtpConfigObject)

Can I use my exsitent schemas for with minimal changes (adding keywords just where i want) be able to hide properties on config json objects (hide or tipical '****' replacing)?

Some ideas?

0 Answers
Related