Mixed schema validation with oneOf

I have collection that contains documents of several “derived classes”, i.e. every document has required properties from “base class” and variation of “derived classes” specific properties. Is it possible to create validator for such schema?
Something like this:

 {
	$jsonSchema: 
	{
		required: 
		[
			'weaponType',
			'active'
		],
		properties: 
		{
			weaponType: 
			{
				enum: ["gun", "knife"],
				description: 'weaponType code'
			},
			active: 
			{
				bsonType: 'bool',
			},
			manufacturer:
			{
				bsonType: 'string'
			},
			oneOf:
			[
				{caliber: {bsonType: 'string'}, clipsize:{bsonType:'int'}},
				{sharpness:{bsonType:'int'}, length:'int'}
			]
		},
		dependencies: {}
	}
}

Hi @14882Dude_Lev ,

I guess for this purpose you can use. Operators like $or in the validator where it’s either one rule or the other :

Thanks

I meant, one part should be fixed (it’s same in all cases) and one part should be one of the followed options.

Isn’t or on the non mandatory side solve it?

Or one set or the other…

Solution: js script that implements all logic and creates final schema definition.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.