0

i need to set default value in enum, if UI passing string is passing empty or null or ""

"BankAccount":{ "type": "String", "enum": ["Y", "N"], "default" = "N" }

in UI passing the BankAccount:"" or null

her default value is not setting

1 Answer 1

0

there are 2 problems:

import Ajv from "ajv" const schema = { type: 'object', properties: { BankAccount: { type: 'string', // string with small s enum: ['Y', 'N'], default: 'N' } } }; const ajv = new Ajv({ useDefaults: 'empty' // treat "" and null as “missing” }); const validate = ajv.compile(schema); const data1 = {}; // BankAccount missing const data2 = { BankAccount: '' }; // empty string const data3 = { BankAccount: null }; // null const data4 = { BankAccount: 'Y' }; // valid value const data5 = { BankAccount: 'N' }; // valid value [ data1, data2, data3, data4, data5 ].forEach((d, i) => { validate(d); // mutates d in‑place console.log(`data${i+1}:`, d); }); // data1: { BankAccount: 'N' } // data2: { BankAccount: 'N' } // data3: { BankAccount: 'N' } // data4: { BankAccount: 'Y' } // data5: { BankAccount: 'N' } 
Sign up to request clarification or add additional context in comments.

2 Comments

Just fyi, the useDefaults is not JSON Schema spec compliant and is not expected to be compliant with any other JSON Schema validator if you ever decide to switch
yes, mutation is bad, in other libs need to go via validation error ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.