5

I'm using AJV library to validate my JSON schema. I want to be able to validate Startdate to be a string. In the case where it is not a string, it should be converted to N/A. Currently, it only converts undefined to N/A.

However, in these cases it does not work as expected:

  • null -> "null"
  • 0 --> "0"
  • true --> "true"

If I want all of the above to be converted to an N/A string, what would my customKeyword function look like?

JSON response:

jsonResponse: {
  "Issue": {
    "StartDate": "December 17, 1995 03:24:00"
  }
}

schema:

var ajv = new Ajv({
    useDefaults: true,
    coerceTypes: 'undefined'
});

const schema = {
    "type": "object",
    "properties": {
        "Issue": {
            "type": "object",
            "properties": {
                "StartDate": {
                    "type": "string"
                    "default": "N/A",
                    "stringTypeChecker"
                }
            }
        }
    }
}

addKeyword function:

ajv.addKeyword('stringTypeChecker', {
  modifying: true,
  validate: function(){
    let foo = []
    console.log(foo)
  }
});

var valid = ajv.validate(schema, jsonResponse);
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
eagercoder
  • 526
  • 1
  • 10
  • 23

1 Answers1

9

You don't need coerceTypes option.

The keyword needs to be:

ajv.addKeyword('stringTypeChecker', {
  modifying: true,
  schema: false, // keyword value is not used, can be true
  valid: true, // always validates as true
  validate: function(data, dataPath, parentData, parentDataProperty){
    if (typeof data != 'string' && parentData) // or some other condition
      parentData[parentDataProperty] = 'N/A';
  }
});

schema:

{
  "type": "object",
  "properties": {
    "Issue": {
      "type": "object",
      "properties": {
        "StartDate": {
          "type": "string",
          "default": "N/A",    
          "stringTypeChecker": true
        }
      }
    }
  }
}
esp
  • 7,314
  • 6
  • 49
  • 79
  • Would the schema value of closing date, be a reference to `stringTypeChecker`? In other words how would I need to change my schema, to use the `stringTypeChecker` keyword? – eagercoder Mar 15 '17 at 20:40
  • 1
    "stringTypeChecker" should be one of schema properties with any value (e.g. `true`) – esp Mar 15 '17 at 21:13
  • The `validate` method inside of `stringTypeChecker` takes four parameters, but you are not using `dataPath` in the conditional logic. This is how I'm calling `stringTypeChecker` --> `var validate = ajv.compile(JSON_SCHEMA);` `var valid = ajv.validate(responseJson, responseJson["Issue"],` `"StartDate" );` Under the hood I observed the validate method expects all four parameters. Is there a way of altering the amount of parameters expected in the validate function, since `validate` does not use `dataPath`? Thank you for the punctual responses. – eagercoder Mar 15 '17 at 21:29
  • 1
    Not sure I understand why you need to change the parameters. It is the api that is documented - see custom keywords docs. – esp Mar 15 '17 at 21:34
  • you are not using it correctly btw, please see the docs (Getting started) – esp Mar 15 '17 at 21:35