Skip to content

Commit 95ca007

Browse files
committed
Schema validation deprecated
Schema validation deprecated
1 parent 487db5f commit 95ca007

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

lib/ajValidation/ajvValidation.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ function getDraftToUse(localDraft, jsonSchemaDialect) {
5050
return localDraft ? localDraft : jsonSchemaDialect;
5151
}
5252

53+
/**
54+
* Checks if the schema object property is deprecated
55+
*
56+
* @param {*} schema - Schema object used in validation
57+
* @returns {Boolean} postman variable or not
58+
*/
59+
function isDeprecated (schema) {
60+
return schema ? schema.deprecated ? schema.deprecated : false : false;
61+
}
62+
5363
/**
5464
* Used to validate schema against a value.
5565
* NOTE: Used in assets/json-schema-faker.js to validate schema example
@@ -107,6 +117,18 @@ function validateSchema (schema, valueToUse, options = {}, jsonSchemaDialect) {
107117
return !isTypeValue(valueToValidate, schemaToUse);
108118
}
109119

120+
if (validationError.keyword === 'required') {
121+
let schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(validationError.schemaPath)),
122+
propertyError = _.get(validationError, 'params.missingProperty') || '',
123+
schemaPropertyPath = schemaDataPath.replace('.required', '.properties.') + propertyError;
124+
125+
if (options.includeDeprecated === false &&
126+
isDeprecated(_.get(schema, schemaPropertyPath))) {
127+
return false;
128+
}
129+
return true;
130+
}
131+
110132
return true;
111133
});
112134

test/unit/xajvValidation.test.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,163 @@ describe('validateSchema', function () {
500500
expect(result[0].keyword).to.equal('enum');
501501
expect(result[1].keyword).to.equal('pattern');
502502
});
503+
504+
it('Should not report error with deprecated property when is present', function () {
505+
const schema = {
506+
type: 'object',
507+
properties: {
508+
deprecated: {
509+
type: 'boolean'
510+
},
511+
pet: {
512+
type: 'object',
513+
properties: {
514+
id: {
515+
type: 'string',
516+
deprecated: true,
517+
default: '<string>'
518+
},
519+
newID: {
520+
type: 'string',
521+
default: '<string>'
522+
},
523+
deprecated: {
524+
type: 'string',
525+
default: '<string>'
526+
}
527+
}
528+
}
529+
}
530+
},
531+
valueToUse = {
532+
deprecated: false,
533+
pet: {
534+
id: '<string>',
535+
newID: '122',
536+
deprecated: 'value'
537+
}
538+
},
539+
result = validateSchema(schema, valueToUse);
540+
expect(result).to.be.empty;
541+
});
542+
543+
it('Should report error with deprecated property when is invalid type', function () {
544+
const schema = {
545+
type: 'object',
546+
properties: {
547+
deprecated: {
548+
type: 'boolean'
549+
},
550+
pet: {
551+
type: 'object',
552+
properties: {
553+
id: {
554+
type: 'string',
555+
deprecated: true,
556+
default: '<string>'
557+
},
558+
newID: {
559+
type: 'string',
560+
default: '<string>'
561+
},
562+
deprecated: {
563+
type: 'string',
564+
default: '<string>'
565+
}
566+
}
567+
}
568+
}
569+
},
570+
valueToUse = {
571+
deprecated: false,
572+
pet: {
573+
id: 2,
574+
newID: '122',
575+
deprecated: 'value'
576+
}
577+
},
578+
result = validateSchema(schema, valueToUse);
579+
expect(result[0].instancePath).to.equal('/pet/id');
580+
expect(result[0].keyword).to.equal('type');
581+
});
582+
583+
it('Should not report error with deprecated property when is not present and' +
584+
' includeDeprecated is false', function () {
585+
const schema = {
586+
type: 'object',
587+
properties: {
588+
deprecated: {
589+
type: 'boolean'
590+
},
591+
pet: {
592+
type: 'object',
593+
required: ['id'],
594+
properties: {
595+
id: {
596+
type: 'string',
597+
deprecated: true,
598+
default: '<string>'
599+
},
600+
newID: {
601+
type: 'string',
602+
default: '<string>'
603+
},
604+
deprecated: {
605+
type: 'string',
606+
default: '<string>'
607+
}
608+
}
609+
}
610+
}
611+
},
612+
valueToUse = {
613+
deprecated: false,
614+
pet: {
615+
newID: '122'
616+
}
617+
},
618+
result = validateSchema(schema, valueToUse, { includeDeprecated: false });
619+
expect(result).to.be.empty;
620+
});
621+
622+
it('Should report error with deprecated property when is not present and' +
623+
' includeDeprecated is true', function () {
624+
const schema = {
625+
type: 'object',
626+
properties: {
627+
deprecated: {
628+
type: 'boolean'
629+
},
630+
pet: {
631+
type: 'object',
632+
required: ['id'],
633+
properties: {
634+
id: {
635+
type: 'string',
636+
deprecated: true,
637+
default: '<string>'
638+
},
639+
newID: {
640+
type: 'string',
641+
default: '<string>'
642+
},
643+
deprecated: {
644+
type: 'string',
645+
default: '<string>'
646+
}
647+
}
648+
}
649+
}
650+
},
651+
valueToUse = {
652+
deprecated: false,
653+
pet: {
654+
newID: '122'
655+
}
656+
},
657+
result = validateSchema(schema, valueToUse, { includeDeprecated: true });
658+
expect(result).to.not.be.empty;
659+
});
503660
});
504661

505662
describe('getDraftToUse', function() {

0 commit comments

Comments
 (0)