![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Data validation and modelling library for those of us with better things to do.
Obey can be installed via NPM: npm install obey --save
Rules are core definitions of how a value should be validated:
import obey from 'obey'
const firstName = obey.rule({ type: 'string', min: 2, max: 45, required: true })
A rule creates a reference which then can be validated with data:
firstName.validate('John')
.then((data) => {
// Passes back `data` value, includes any defaults set,
// generated, or modified data
})
.catch(error => {
// Returns instance of ValidationError
// `error.message` => String format error messages
// `error.collection` => Raw array of error objects
})
Models allow for creating validation rules for entire object schemas. The following demonstrates a basic model being created with Obey:
import obey from 'obey'
const userModel = obey.model({
id: { type: 'uuid', generator: 'uuid', required: true },
email: { type: 'email', required: true },
password: { type: 'string', modifier: 'encryptPassword', required: true }
fname: { type: 'string', description: 'First Name' },
lname: { type: 'string', description: 'Last Name' },
phone: { type: 'phone', min: 7, max: 10 },
// Array
labels: { type: 'array', values: {
type: 'object', keys: {
label: { type: 'string' }
}
}},
// Nested object
address: { type: 'object', keys: {
street: { type: 'string', max: 45 },
city: { type: 'string', max: 45 }
state: { type: 'string', max: 2, modifier: 'upperCase' },
zip: { type: 'number', min: 10000, max: 99999 }
}},
// Key-independent object validation
permissions: { type: 'object', values: {
type: 'string'
},
account: { type: 'string', allow: [ 'user', 'admin' ], default: 'user' }
})
Using the example above, validation is done, similar to validating with a rule, by calling the validate
method and supplying data:
userModel.validate({ /* some data object */ })
.then((data) => {
// Passes back `data` object, includes any defaults set,
// generated, or modified data
})
.catch(error => {
// Returns instance of ValidationError
// `error.message` => String format error messages
// `error.collection` => Raw array of error objects
})
The validate method returns a promise (for more information see [Asynchronous Validation](#Asynchronous Validation)). A passing run will resolve with the data, any failures will reject and the ValidationError
instance will be returned.
The properties used can each be explained as:
type
: The type of value, either native or custom, see Typeskeys
: Property of object
type, indicates nested object propertiesvalues
: Defines value specification for arrays or key-independent object testsmodifier
: uses a method and accepts a passed value to modify or transform data, see Modifiersgenerator
: uses a method to create a default value if no value is supplied, see Generatorsdefault
: The default value if no value specifiedmin
: The minimum character length for a string, lowest number, or minimum items in arraymax
: The maximum character length for a string, highest number, or maximum items in arrayrequired
: Enforces the field cannot be missing during validationallow
: Array of allowed values or single allowed valuestrict
: Enable or disable strict checking of an object, see Strict Modedescription
: A description of the propertyBy default, Obey enforces strict matching on objects; meaning an object must define any keys that will be present in the data object being validated.
To disable strict mode on a rule or object set the strict
property to false:
foo: { type: 'object', strict: false, keys: { /* ... */ } }
To disable strict mode on a model pass the (optional) strict argument as false
:
const model = obey.model({ /* definition */ }, false)
Types are basic checks against native types, built-ins or customs. The library includes native types (boolean
, null
, undefined
, number
, string
, array
, and object
) as well other common types. A list of built-in types is contained in the source.
New types can be added to the Obey lib with the obey.type
method:
obey.type('lowerCaseOnly', context => {
if (!context.value.test(/^([a-z])*$/) {
context.fail(`${context.key} must be lowercase`)
}
})
The second argument is the method to run validation and gets passed a context
object by the library. This object has the following properties:
def
: The entire rule for the property in the modelkey
: The name of the property being tested (if an element in a model/object)value
: The value to testfail
: A function accepting a failure message as an argumentThe above would add a new type which would then be available for setting in the model configuration for any properties.
label: { type: 'lowerCaseOnly', /* ...additional config... */ }
Types can be synchronous or asynchronous. Types can return/resolve a value, though it is not required and is recommended any coercion be handled with a modifier.
Regardless of if a value is returned/resolved, asynchronous types must resolve. Errors should be handled with the context.fail()
method.
Modifiers allow custom methods to return values which are modified/transformed versions of the received value.
Modifiers can be added to the Obey lib with the obey.modifier
method:
obey.modifier('upperCase', val => val.toUpperCase())
When the model is validated, the value in any fields with the upperCase
modifier will be transformed to uppercase.
Modifiers can be synchronous or asynchronous. In both cases they must either return (or resolve) the final value.
Generators allow custom methods to return values which set the value similar to the default
property. When validating, if a value is not provided the generator assigned will be used to set the value.
Generators can be added to the Obey lib with the obey.generator
method:
obey.generator('timestamp', () => new Date().getTime())
The above example would add a generator named timestamp
which could be assigned as shown below:
created: { type: 'number', generator: 'timestamp' }
When the model is validated, if no created
property is provided the timestamp
generator will assign the property a UTC timestamp.
Generators can be synchronous or asynchronous. In both cases they must either return (or resolve) the final value.
The goal with Obey is to provide more than just standard type/regex checks against data to validate values and models. The ability to write both synchronous and asynchronous checks, generators, and modifiers, and include data coercion in the validation simplifies the process of validation and checking before moving onto data source interactions.
Additionally, with the widespread use of promises, this structure fits well in the scheme of data processing in general:
// Define a model somewhere in your code...
const user = obey.model(/* ...Model Definition... */)
// Use it to validate before creating a record...
user.validate(/* ...some data object... */)
.then(createUser)
.then(/* ...response or other action... */)
.catch(/* ...handle errors... */)
Obey is developed and maintained by TechnologyAdvice and released under the MIT license.
FAQs
Data modelling and validation library
The npm package obey receives a total of 147 weekly downloads. As such, obey popularity was classified as not popular.
We found that obey demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.