JSON SCHEMA
Similar to XSD for XML, can describe json data & schema validations.
Schema Specification -> JSON Schema Validation
Go tolet $jsonSchema = {
type: "object",
required: ["tenderId", "tenderNotificationNumber", "submissionDate"],
properties: {
tenderId: {
type: "int", //double
minimum: 100,
maximum: 99999,
description: "must be an integer in [ 100, 99999 ] and is required"
},
tenderNotificationNumber: {
type: "string",
minLength: 3,
maxLength: 500
description: "must be a string and is required"
},
submissionDate: {
type: "string",
format: "date-time", //date, time, email
description: "must be an date and is required"
},
tenderStatus: {
enum: [1, 2, 3, 10],
description: "can only be one of the enum values"
},
EMDDetails: {
type: "object",
required: ["Amount"],
properties: {
Amount: {
type: "int",
"description": "must be a integer and is required"
},
FeeType: {
type: "string",
description: "must be a string"
}
}
},
AliasNames: {
type: "array",
items: {
type: "string"
},
minItems: 2,
maxItems: 3,
uniqueItems: true
},
tenderDocuments: {
type: "array",
items: {
type: "object",
properties: {
fileName: {
type: "string"
},
fileExtension: {
type: "string"
}
}
}
}
}
}
}
In MongoDB
- In validator object -> $jsonSchema is also supported (just type = bsonType)
- But description error not supported yet
- Native IsoDate must be stored,
- currently till draft 4 only
- Date validations needed to be handled separately not from JSON schema yet
Useful links
- SPEC : https://json-schema.org/ & https://github.com/json-schema-org/json-schema-spec
- MongoDB : https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/
- LIBRARY :
- ajv,
- relative data reference
- also check ajv custom keywords
- proposed, draft7 if else