
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
Condensed Syntax
var schema = new Schema({
name:String,
age:Number,
phones:[{ number:String, mobile:Boolean }]
})
Explicit Syntax
var person = new Schema({
type: Object,
properties: {
name: { type:String },
age: { type:Number },
phones: {
type: Array,
items: {
type: Object,
properties: {
number: { type:String },
mobile: { type:Boolean }
})
}
}
}
})
These two syntaxes may be mixed within the same schema definition. The expanded syntax is required to specify additional options (instead of just types, array items, and object properties) for a field.
new Schema({
name: {
first: { type:String, name:'First Name', required:true },
last: { type:String, name:'Last Name', required:true }
},
age: { type:Number, default:18 }
})
type Function|Array<Function>
The constructor function for the type of the value. May use javascript types or other constructor functions (String, Number, Date, bson.ObjectID, Schema, etc.) or an Array of constructor functions.
default
The default value for a field if no value is specfied.
The type of the default value must match the above type.
required Boolean|Object
If type is Object, an Array listing required properties.
If type is not Object, a Boolean indicating whether a value is required for this field.
properties Object
Required if type is Object. Enforces the schema on the subkeys of this field.
items Object
Required if type is Array. Enforces the schema on all elements of the Array.
validators Array<Function>
Array of custom validator functions matching the signature function(value). A validator should throw if it fails, otherwise return.
var schema = new Schema({ name:String, age:Number })
schema.validate({ name:'John Doe', age:27 })
//returns true
schema.validate({ name:'John Doe', age:'old' })
//errors because age is invalid. "old" is not a Number
###new Schema(definition)
definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
schema.validate(value)value
An value to check against the schema. See Validating an Object.
Schema.applyDefaults(value)value
A value to which the defaults for the schema will be applied.
schema.extend(definition)definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
schema.extendWhen(query, definition)query
An object defining a MongoDB-like query. Powered by sift. Supports the following operators: $in, $nin, $exists, $gte, $gt, $lte, $lt, $eq, $neq, $mod, $all, $and, $or, $nor, $not, $size, $type, $regex.
definition
An object defining fields to be added to the schema. See Defining a Schema and Field Options.
schema.extendWhen() allows you to modify the schema based on the values of the object that you are validating. For example, we can require a credit card number and cvv, but only if the purchase is being made by credit card.
var purchase = new Schema({ amount:Number, payment:String })
purchase.extendWhen({ payment:'credit' }, {
card: {
number: { type:Number, required:true },
cvv: { type:Number, required:true }
}
})
purchase.extendWhen({ payment:'check' }, {
check:{
account: { type:Number, required:true },
routing: { type:Number, required:true }
}
})
purchase.validate({ amount:10.00, payment:'credit' })
// errors because card.number and card.cvv are required
purchase.validate({ amount:10.00, payment:'cash', card: { number:4716740357239704 })
// errors because card.number is not present in the base schema
purchase.validate({ amount:10.00, payment:'check', check:{ account:9900000003, routing:321174851 })
// returns true
type:
var schema = new Schema({ result:[Boolean|Function] })
schema.extendWhen({ result:{ $type:Boolean } }, {
additional:Number
})
comparisons:
var purchase = new Schema({ amount:Number })
purchase.extendWhen({ amount:{ $gt:0 } }, {
payment: { type:String, required:true }
})
var person = new Schema({
name: {
first:String,
middle:String,
last: { type:String, required:true }
}
})
person.extendWhen({ 'name.middle': { $exists:false } }), {
name: {
first:{ type:String, required:true }
}
})
FAQs
Simple but powerful library to validate any javascript value
The npm package schema-js receives a total of 91 weekly downloads. As such, schema-js popularity was classified as not popular.
We found that schema-js 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.