Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
is-descriptor
Advanced tools
Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.
The is-descriptor npm package is used for checking if an object is a valid descriptor. Descriptors are objects that define the behavior of properties on JavaScript objects, such as whether they are writable, enumerable, or configurable, and they can also specify getter and setter functions. This package is useful for validation in libraries that manipulate object properties and descriptors, ensuring that operations like defining new properties or modifying existing ones are performed with valid descriptor objects.
Checking property descriptors
This feature allows you to check if a given object is a valid property descriptor. The code sample demonstrates checking various objects to see if they qualify as descriptors based on their properties.
const isDescriptor = require('is-descriptor');
console.log(isDescriptor({enumerable: false, configurable: true})); // true
console.log(isDescriptor({value: 'hello'})); // true
console.log(isDescriptor({get: function() {}})); // true
console.log(isDescriptor({set: undefined})); // false
This package checks specifically for data descriptors, which are a type of property descriptor focusing on value properties rather than accessor properties (getters and setters). It's more specific compared to is-descriptor, which checks for both data and accessor descriptors.
Similar to is-data-descriptor, this package focuses on checking for accessor descriptors, which define getter and setter functions for a property. It complements is-data-descriptor by covering the other main type of property descriptor, offering a more focused approach compared to the broader checks performed by is-descriptor.
Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for fully completed data descriptors and accessor descriptors.
const isDescriptor = require('is-descriptor');
const assert = require('assert');
const defaults = { configurable: false, enumerable: false };
const dataDefaults = { ...defaults, writable: false};
assert.ok(isDescriptor({ ...dataDefaults, value: 'foo' }));
assert.ok(isDescriptor({ ...defaults, get() {}, set() {} }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set() {} }));
You may also check for a descriptor by passing an object as the first argument and property name (string
) as the second argument.
const obj = { foo: 'abc' };
Object.defineProperty(obj, 'bar', { value: 'xyz' });
Reflect.defineProperty(obj, 'baz', { value: 'xyz' });
assert.equal(isDescriptor(obj, 'foo'), true);
assert.equal(isDescriptor(obj, 'bar'), true);
assert.equal(isDescriptor(obj, 'baz'), true);
Returns false
when not an object
assert.equal(isDescriptor('a'), false);
assert.equal(isDescriptor(null), false);
assert.equal(isDescriptor([]), false);
Returns true
when the object has valid properties with valid values.
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo' }), true);
assert.equal(isDescriptor({ ...dataDefaults, value() {} }), true);
Returns false
when the object has invalid properties
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', get() {} }), false);
assert.equal(isDescriptor({ ...dataDefaults, get() {}, value() {} }), false);
false
when a value is not the correct type
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', writable: 'foo' }), false);
true
when the object has valid properties with valid values.
assert.equal(isDescriptor({ ...defaults, get() {}, set() {} }), true);
assert.equal(isDescriptor({ ...defaults, get() {} }), true);
assert.equal(isDescriptor({ ...defaults, set() {} }), true);
false
when the object has invalid properties
assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, writable: true }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, value: true }), false);
Returns false
when an accessor is not a function
assert.equal(isDescriptor({ ...defaults, get() {}, set: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set() {} }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set: 'baz' }), false);
Returns false
when a value is not the correct type
assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, set() {}, configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, configurable: 'foo' }), false);
You might also be interested in these projects:
Simply clone the repo, npm install
, and run npm test
v3.1.1 - 2023-10-27
gopd
and hasown
d32fc37
v0.1.7
, v1.0.3`` [
596c888`](https://github.com/inspect-js/is-descriptor/commit/596c888a558420697a396947cd24d0edd42e19ef)8bcf028
544cdfe
1130f79
3f8f094
92ee1bf
8da3a3c
set
and not get
is still an accessor descriptor 269fb53
files
to exports
41b2d61
9fd1ac8
@ljharb/eslint-config
, aud
, tape
52668c9
is-accessor-descriptor
, is-data-descriptor
f4dbc73
5f4b0e1
91be723
9eaa17c
FAQs
Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.
The npm package is-descriptor receives a total of 29,930,679 weekly downloads. As such, is-descriptor popularity was classified as popular.
We found that is-descriptor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.