1.2.2 • Published 3 years ago
is-my-ssb-valid v1.2.2
is-my-ssb-valid
Build message validators for scuttlebutt using JSON schema.
Uses is-my-json-valid under the hood
Example usage
const Validator = require('is-my-ssb-valid')
const profileSchema = require('ssb-profile/spec/profile/schema')
const isProfile = Validator(profileSchema)const A = {
//....
}
isProfile(A)
// => true | false
console.log(isProfile.errors)
// => null | [Error]
console.log(isProfile.errorsString)
// => '' | 'data.type is required' (example)const pull = require('pull-stream')
pull(
sbot.messagesByType({ type: 'profile/person', reverse: true }),
pull.filter(isProfile),
pull.take(50),
pull.collect((err, profileUpdates) => {
// ...
})
)API
Validator(schema) => isValid
where:
schemaObject is a JSON schemaextrasArray (optional) is an Array of additional custom validation functions.- These will be run after the schema-based validation, and are expected to either return
true(no problem) or anError(a problem) was found. - any error returned be added to the
isValid.errorsandisValid.errorsStringfor you (see below)
- These will be run after the schema-based validation, and are expected to either return
isValid(msg) => Boolean
where msg can be any of
- a full message a "full message" of form
{ key, value, timestamp } - just the
valueof a message (msg.value) - just the
contentfield of a message (msg.value.content)
If the last message tested came back invalid ("false"), then you can also check out the specific errors:
isValid.errorswill be populated with those errorsisValid.errorsStringwill be populated with a (crudely) flattened version in case you need a String