How to test that a JavaScript object has only specific fields/properties











up vote
0
down vote

favorite












I have this function which tests that an object has certain fields



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result != undefined) && (res['additional-info'] != undefined
);
return isTypeCorrect;
}


Is there a way to test that res has only result and additional-info properties and nothing else?










share|improve this question
























  • You should have used in operator instead: eg: 'result' in res
    – zerkms
    Nov 11 at 7:09

















up vote
0
down vote

favorite












I have this function which tests that an object has certain fields



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result != undefined) && (res['additional-info'] != undefined
);
return isTypeCorrect;
}


Is there a way to test that res has only result and additional-info properties and nothing else?










share|improve this question
























  • You should have used in operator instead: eg: 'result' in res
    – zerkms
    Nov 11 at 7:09















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this function which tests that an object has certain fields



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result != undefined) && (res['additional-info'] != undefined
);
return isTypeCorrect;
}


Is there a way to test that res has only result and additional-info properties and nothing else?










share|improve this question















I have this function which tests that an object has certain fields



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result != undefined) && (res['additional-info'] != undefined
);
return isTypeCorrect;
}


Is there a way to test that res has only result and additional-info properties and nothing else?







javascript typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:17









Roy Scheffers

2,11281625




2,11281625










asked Nov 11 at 6:56









Manu Chadha

2,61611235




2,61611235












  • You should have used in operator instead: eg: 'result' in res
    – zerkms
    Nov 11 at 7:09




















  • You should have used in operator instead: eg: 'result' in res
    – zerkms
    Nov 11 at 7:09


















You should have used in operator instead: eg: 'result' in res
– zerkms
Nov 11 at 7:09






You should have used in operator instead: eg: 'result' in res
– zerkms
Nov 11 at 7:09














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Just check that the length of its keys is exactly two. Also, probably best to use strict equality comparison, if you want to ensure that the properties are not undefined:



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result !== undefined
&& res['additional-info'] !== undefined
&& Object.keys(res).length === 2
);
return isTypeCorrect;
}


In the unusual case that you may have properties that contain the undefined value, and you want to permit such values, check the length of the keys, and that both result and additional-info are included in the keys:



const keys = Object.keys(res);
let isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2
)





share|improve this answer























  • You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
    – Tsahi Asher
    Nov 11 at 7:08










  • res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
    – zerkms
    Nov 11 at 7:08












  • @TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
    – CertainPerformance
    Nov 11 at 7:17












  • @zerkms - I don't understand your comment.
    – Manu Chadha
    Nov 11 at 16:52










  • @ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
    – zerkms
    Nov 11 at 22:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246514%2fhow-to-test-that-a-javascript-object-has-only-specific-fields-properties%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Just check that the length of its keys is exactly two. Also, probably best to use strict equality comparison, if you want to ensure that the properties are not undefined:



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result !== undefined
&& res['additional-info'] !== undefined
&& Object.keys(res).length === 2
);
return isTypeCorrect;
}


In the unusual case that you may have properties that contain the undefined value, and you want to permit such values, check the length of the keys, and that both result and additional-info are included in the keys:



const keys = Object.keys(res);
let isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2
)





share|improve this answer























  • You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
    – Tsahi Asher
    Nov 11 at 7:08










  • res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
    – zerkms
    Nov 11 at 7:08












  • @TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
    – CertainPerformance
    Nov 11 at 7:17












  • @zerkms - I don't understand your comment.
    – Manu Chadha
    Nov 11 at 16:52










  • @ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
    – zerkms
    Nov 11 at 22:37















up vote
2
down vote



accepted










Just check that the length of its keys is exactly two. Also, probably best to use strict equality comparison, if you want to ensure that the properties are not undefined:



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result !== undefined
&& res['additional-info'] !== undefined
&& Object.keys(res).length === 2
);
return isTypeCorrect;
}


In the unusual case that you may have properties that contain the undefined value, and you want to permit such values, check the length of the keys, and that both result and additional-info are included in the keys:



const keys = Object.keys(res);
let isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2
)





share|improve this answer























  • You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
    – Tsahi Asher
    Nov 11 at 7:08










  • res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
    – zerkms
    Nov 11 at 7:08












  • @TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
    – CertainPerformance
    Nov 11 at 7:17












  • @zerkms - I don't understand your comment.
    – Manu Chadha
    Nov 11 at 16:52










  • @ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
    – zerkms
    Nov 11 at 22:37













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Just check that the length of its keys is exactly two. Also, probably best to use strict equality comparison, if you want to ensure that the properties are not undefined:



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result !== undefined
&& res['additional-info'] !== undefined
&& Object.keys(res).length === 2
);
return isTypeCorrect;
}


In the unusual case that you may have properties that contain the undefined value, and you want to permit such values, check the length of the keys, and that both result and additional-info are included in the keys:



const keys = Object.keys(res);
let isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2
)





share|improve this answer














Just check that the length of its keys is exactly two. Also, probably best to use strict equality comparison, if you want to ensure that the properties are not undefined:



validateServerResponseStructure(res: any) {
let isTypeCorrect: boolean = (
res.result !== undefined
&& res['additional-info'] !== undefined
&& Object.keys(res).length === 2
);
return isTypeCorrect;
}


In the unusual case that you may have properties that contain the undefined value, and you want to permit such values, check the length of the keys, and that both result and additional-info are included in the keys:



const keys = Object.keys(res);
let isTypeCorrect: boolean = (
['result', 'additional-info'].every(key => keys.includes(key))
&& keys.length === 2
)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 7:13

























answered Nov 11 at 6:58









CertainPerformance

67.3k143353




67.3k143353












  • You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
    – Tsahi Asher
    Nov 11 at 7:08










  • res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
    – zerkms
    Nov 11 at 7:08












  • @TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
    – CertainPerformance
    Nov 11 at 7:17












  • @zerkms - I don't understand your comment.
    – Manu Chadha
    Nov 11 at 16:52










  • @ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
    – zerkms
    Nov 11 at 22:37


















  • You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
    – Tsahi Asher
    Nov 11 at 7:08










  • res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
    – zerkms
    Nov 11 at 7:08












  • @TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
    – CertainPerformance
    Nov 11 at 7:17












  • @zerkms - I don't understand your comment.
    – Manu Chadha
    Nov 11 at 16:52










  • @ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
    – zerkms
    Nov 11 at 22:37
















You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
– Tsahi Asher
Nov 11 at 7:08




You say " best to use strict equality", but you don't use it. You should use typeof res.result !== "undefined" && typeof res['additional-info'] !== "undefined"
– Tsahi Asher
Nov 11 at 7:08












res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
– zerkms
Nov 11 at 7:08






res.result !== undefined does not check if a property exists, but only if its value is not undefined. { foo: undefined } --- here the .foo property would exist yet it's undefined.
– zerkms
Nov 11 at 7:08














@TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
– CertainPerformance
Nov 11 at 7:17






@TsahiAsher "Strict equality" refers to using === and !== rather than == and !=. Pretty sure adding typeof only adds a bit of syntax noise
– CertainPerformance
Nov 11 at 7:17














@zerkms - I don't understand your comment.
– Manu Chadha
Nov 11 at 16:52




@zerkms - I don't understand your comment.
– Manu Chadha
Nov 11 at 16:52












@ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
– zerkms
Nov 11 at 22:37




@ManuChadha you asked "How to test that a JavaScript object has only specific fields/properties " --- you don't check if an object has a particular property with !== undefined. Equality to undefined has nothing to do with property presence. To check if property presents you need to use in or Object.keys as in this answer
– zerkms
Nov 11 at 22:37


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246514%2fhow-to-test-that-a-javascript-object-has-only-specific-fields-properties%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python