
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
cast an object's values to a specified schema
npm install castaway
Given an object, and a schema defining the types of the objects properties, castaway will convert the values to the appropriate JS primitives.
var cast = require('castaway');
var obj = { foo: '1337'},
schema = { foo: { type: Number }};
cast(obj, schema);
console.log(typeof obj.foo); // number
Supported type
values are:
Arrays also support defining member schemas via an each
property, and specifying the schema or types:
Here's an example of specifying the schema via each
for an array...
var schema = {
foo: {
type: Array,
each: {
schema: {
id: { type: Number },
when: { type: Date }
}
}
}
},
obj = { foo: [
{ id: '1', hee: 'haw', when: '2013-02-28 16:31:13' },
{ id: '2', hee: 'hoo', when: '2013-02-28' }
]};
cast(obj, schema);
// for each entry in foo, id is a Number, when is a Date, hee is still a String
And here's an example of just specifying the type
for an array via each
...
var schema = { foo: { type: Array, each: { type: Number } } },
obj = { foo: '1,2,3,4,5,6,7,8,9' };
cast(obj, schema);
// foo is an array of numbers
FAQs
cast JS objects to a specified schema, ahoy!
The npm package castaway receives a total of 3 weekly downloads. As such, castaway popularity was classified as not popular.
We found that castaway demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.