A Validation is a function that takes a validation library and a document's content as input, and throws a couchDB validation error object if the validation fails. Example:
interface Post {
body: string;
reply: Post;
}
function PostSpec(): DS.ValidationSpec<Post> {
return {
type: "post",
schema: {
body: {
required: true,
validations: [
(lib, body) =>
lib.failIf(
body.length > 10,
"Post must be at most 10 characters"
)
]
},
reply: {
spec: PostSpec
}
}
};
}
NOTE: Validations cannot close over variables outside the function's scope as all scope information is lost after function serialization.
A ValidationSchema Determines how record fields should validate its associated data type.
Given a PouchDB database object and a list of SpecFunction elements, calls defineSpec on each SpecFunction and then inserts an update validation design document into the database that blocks any other document type to be inserted into the database. The following example with throw a validation error.
defineOnly(db, PostSpec)
await db.put({
_id: "invalid-document",
path: [],
type: "non-existent"
})
Given a PouchDB database object and a SpecFunction,
defineSpec will insert an update validation design document
that runs specified validation functions on each inserted document
for defined specification type name.
NOTE:
if the type
field of the document does not match the specified type name,
the validation does not fail. If you want to only allow a specific list of
document types see defineOnly.
Returns the database name of the given PouchDB database. Throws an error if the database is not reachable.
Returns a mango query that filters all documents that match the given $SpecFunction, are nested under the given path, and originate from the . This is helpful for filtered replication
Get the design document id for the update validation for the given SpecFunction.
Gets the view path that can be used by PouchDB query api
Get the design document id for the view that lists all documents specified by the given SpecFunction with paths as keys.
Adds all required pouchdb plugins used by this library to the locally imported PouchDB module. Example:
var PouchDB = require("pouchdb");
setupPlugins(PouchDB);
Creates a DocHandle to work with single document.
Creates a PathHandle to work with documents at the given path.
Creates a PathHandle to work with documents at the root path (empty array). Example:
const root = DS.useRoot(db, PostSpec);
await root
.then(_ => _.create())
.then(_ => _.path.reply())
.then(_ => _.create());
const list = await root.then(_ => _.list());
const replyList = await root
.then(_ => _.find())
.then(_ => _ && _.path.reply())
.then(_ => _ && _.list());
Generated using TypeDoc
Data specifications are functions that return a ValidationSpec