Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
A tool designed to sanity check settings before running your node.js application.
Sanity is a platform for structured content that comes with an open-source editing environment called Sanity Studio. It allows you to build and manage content with a flexible and customizable interface.
Content Modeling
Sanity allows you to define your content models using JavaScript objects. This example shows a simple schema for a blog post with a title and body.
const schema = {
name: 'blogPost',
type: 'document',
fields: [
{ name: 'title', type: 'string' },
{ name: 'body', type: 'text' }
]
};
export default schema;
Real-time Collaboration
Sanity supports real-time collaboration, allowing multiple users to work on the same document simultaneously. This example demonstrates how to use the `useDocumentOperation` hook to update a document in real-time.
import { useDocumentOperation } from '@sanity/react-hooks';
function MyComponent({ id }) {
const { patch, commit } = useDocumentOperation(id, 'myDocumentType');
const handleChange = () => {
patch.execute([{ set: { title: 'New Title' } }]);
commit.execute();
};
return <button onClick={handleChange}>Change Title</button>;
}
Custom Input Components
Sanity allows you to create custom input components for your content models. This example shows how to create a simple custom input component using React.
import React from 'react';
import { FormField } from '@sanity/base/components';
const MyCustomInput = React.forwardRef((props, ref) => {
return (
<FormField label='My Custom Input'>
<input ref={ref} type='text' {...props} />
</FormField>
);
});
export default MyCustomInput;
Contentful is a headless CMS that provides a similar set of features for content modeling and management. It offers a web-based interface for content creators and a robust API for developers. Compared to Sanity, Contentful has a more polished UI but may be less flexible in terms of customization.
Strapi is an open-source headless CMS that allows you to create and manage content with a customizable API. It offers a more developer-friendly experience with its plugin system and is highly customizable. Strapi is similar to Sanity in terms of flexibility but has a different approach to content modeling and API generation.
Prismic is a headless CMS that focuses on providing a user-friendly interface for content creators and a powerful API for developers. It offers features like content versioning and scheduling. Compared to Sanity, Prismic is more focused on ease of use and may have fewer customization options.
Sanity is a small script designed to sanity check settings before running your node.js application. Check for values in the environment or specific objects using provided or custom matchers. If the matcher return true, the value is considered passing. Any non-passing values are reported by printing to the output or via a callback function.
npm install sanity
Here's the signature for sanity:
sanity.check(
['array', 'of', 'keys'],
/* optional data source {gein: 'clown'}, */
/* optional configuration object: {passiveAggressive: true}, */
/* optional callback: function(err, keys){ if(err) alert(keys.join(', ')); } */
)
Check environment variables are set to a non-empty string value
var sanity = require('sanity');
sanity.check(['USER', 'MACHTYPE', 'INVISIBLE_FRIENDS', 'AREA_51']);
// Output if INVISIBLE_FRIENDS and AREA_51 are undefined
ERROR: Required settings are not correct!
INVISIBLE_FRIENDS: undefined
AREA_51: undefined
Use the built-in matchers or provide your own
var sanity = require('sanity');
sanity.check(
[{
key: 'USER',
matcher: 'defined'
},
{
key: 'TRUTH_IS_OUT_THERE',
matcher: 'truthy'
},
{
key: 'THERE_WAS_A_SECOND_SHOOTER',
matcher: 'falsy'
},
{
key: 'TINFOIL_HATS',
matcher: function() {
return TinfiolHats.get('all').length > 42;
}
}]
);
// Theoretical output
ERROR: Required settings are not correct!
THERE_WAS_A_SECOND_SHOOTER: true
TINFOIL_HATS: 3
Provide a key/value data source to use other than environment variables
var sanity = require('sanity');
sanity.check(
['GOTTI_BURIAL_LOCATION', 'UNDERCOVER_AGENT'],
{
GOTTI_BURIAL_LOCATION: app.unveilTruth('gotti'),
UNDERCOVER_AGENT: db.get('user', 'type = "undercover"')
});
// Theoretical output
ERROR: Required settings are not correct!
GOTTI_BURIAL_LOCATION: undefined
Define what happens if values are matches are not true
var sanity = require('sanity');
sanity.check(['ONE_ARMED_MAN'], null, {
gagged: true, // default: false
passiveAggressive: true // default: false
});
// ONE_ARMED_MAN is falsy but `gagged` prevents logging and `passiveAggressive` does not exit the process
Supply a callback if you want to control the application flow after checking
var sanity = require('sanity');
sanity.check(['UFOS'], null, null, function(err, keys) {
console.error(err); // Same error format as seen before
console.log(keys) // Array of keys which did not pass
process.exit(1);
});
There are a few options to change how sanity behaves.
process.exit(1)
when errors are found.To run the tests make sure you have jasmine-node installed globally, then run this command from the sanity
folder you cloned into:
jasmine-node test/
FAQs
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
The npm package sanity receives a total of 107,251 weekly downloads. As such, sanity popularity was classified as popular.
We found that sanity demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 62 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.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.