Class: Schema

Schema


new Schema(definition)

js-data's Schema class.

Method parameters:
Name Type Description
definition Object

Schema definition according to json-schema.org

Details
Source
Schema.js, line 993
Example
Schema#constructor</caption>
// Normally you would do:  import {Schema} from 'js-data'
const JSData = require('js-data@3.0.0-beta.7')
const {Schema} = JSData
console.log('Using JSData v' + JSData.version.full)

const PostSchema = new Schema({
  type: 'object',
  properties: {
    title: { type: 'string' }
  }
})
PostSchema.validate({ title: 1234 })

Extends

This class extends the Component class.

Members


<static> typeGroupValidators

A map of validation functions grouped by type.

Details
Type Source
Object Schema.js, line 873

<static> types

A function map for each of the seven primitive JSON types defined by the core specification. Each function will check a given value and return true or false if the value is an instance of that type.

  types.integer(1) // returns true
  types.string({}) // returns false

http://json-schema.org/latest/json-schema-core.html#anchor8

Details
Type Source
Object Schema.js, line 6

<static> validationKeywords

A map of all object member validation functions for each keyword defined in the JSON Schema.

Details
Type Source
Object Schema.js, line 96

_listeners

Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.

Details
Type Since Source
Object 3.0.0 Component.js, line 52
Inherited From:

debug

Whether to enable debug-level logs for this component. Anything that extends Component inherits this option and the corresponding logging functionality.

Details
Type Since Default value Source
Boolean 3.0.0
false
Component.js, line 28
Inherited From:
Example

// Normally you would do: import {Component} from 'js-data'
const JSData = require('js-data@3.0.0-beta.7')
const {Component} = JSData
console.log('Using JSData v' + JSData.version.full)

const component = new Component()
component.log('debug', 'some message') // nothing gets logged
// Display debug logs:
component.debug = true
component.log('debug', 'other message') // this DOES get logged

Methods


<static> extend(props, classProps)

Create a subclass of this Schema:

Method parameters:
Name Type Argument Default Description
props Object <optional>
{}

Properties to add to the prototype of the subclass.

Properties
Name Type Argument Description
constructor Object <optional>

Provide a custom constructor function to be used as the subclass itself.

classProps Object <optional>
{}

Static properties to add to the subclass.

Return value:
Type Description
Constructor

Subclass of this Schema class.

Details
Since Source
3.0.0 Schema.js, line 1087
Example

// Normally you would do: import {Schema} from 'js-data'
const JSData = require('js-data@3.0.0-beta.7')
const {Schema} = JSData
console.log('Using JSData v' + JSData.version.full)

// Extend the class using ES2015 class syntax.
class CustomSchemaClass extends Schema {
  foo () { return 'bar' }
  static beep () { return 'boop' }
}
const customSchema = new CustomSchemaClass()
console.log(customSchema.foo())
console.log(CustomSchemaClass.beep())

// Extend the class using alternate method.
const OtherSchemaClass = Schema.extend({
  foo () { return 'bar' }
}, {
  beep () { return 'boop' }
})
const otherSchema = new OtherSchemaClass()
console.log(otherSchema.foo())
console.log(OtherSchemaClass.beep())

// Extend the class, providing a custom constructor.
function AnotherSchemaClass () {
  Schema.call(this)
  this.created_at = new Date().getTime()
}
Schema.extend({
  constructor: AnotherSchemaClass,
  foo () { return 'bar' }
}, {
  beep () { return 'boop' }
})
const anotherSchema = new AnotherSchemaClass()
console.log(anotherSchema.created_at)
console.log(anotherSchema.foo())
console.log(AnotherSchemaClass.beep())

<static> validate(value, schema, opts)

Validates the provided value against a given Schema according to the http://json-schema.org/ v4 specification.

Method parameters:
Name Type Argument Description
value *

Value to be validated.

schema Object

Valid Schema according to the http://json-schema.org/ v4 specification.

opts Object <optional>

Configuration options.

Return value:
Type Description
Array | undefined

Array of errors or undefined if valid.

Details
Source
Schema.js, line 672

dbg(args)

Log the provided values at the "debug" level. Debug-level logs are only logged if Component#debug is true.

.dbg(...) is shorthand for .log('debug', ...).

Method parameters:
Name Type Argument Description
args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 123
Inherited From:

emit(event, args)

Trigger an event on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to emit.

args * <optional>
<repeatable>

Arguments to pass to any listeners.

Details
Since Source
3.0.0 Component.js, line 201
Inherited From:
Example

// import {Collection, DataStore} from 'js-data'
const JSData = require('js-data@3.0.0-beta.7')
const {Collection, DataStore} = JSData

const collection = new Collection()
collection.on('foo', (msg) => {
  console.log(msg)
})
collection.emit('foo', 'bar')

const store = new DataStore()
store.on('beep', (msg) => {
  console.log(msg)
})
store.emit('beep', 'boop')

log(level, args)

Log the provided values. By default sends values to console[level]. Debug-level logs are only logged if Component#debug is true.

Will attempt to use appropriate console methods if they are available.

Method parameters:
Name Type Argument Description
level String

Log level.

args * <optional>
<repeatable>

Values to log.

Details
Since Source
3.0.0 Component.js, line 133
Inherited From:

off(event, listener)

Remove an event listener from this Component. If no listener is provided, then all listeners for the specified event will be removed. If no event is specified then all listeners for all events will be removed.

Method parameters:
Name Type Argument Description
event String <optional>

Name of event to unsubsribe to.

listener Function <optional>

Listener to remove.

Details
Since Source
3.0.0 Component.js, line 179
Inherited From:
Examples
// Remove a particular listener for a particular event
collection.off('add', handler)
// Remove all listeners for a particular event
record.off('change')
// Remove all listeners to all events
store.off()

on(event, listener, ctx)

Register a new event listener on this Component.

Method parameters:
Name Type Argument Description
event String

Name of event to subsribe to.

listener Function

Listener function to handle the event.

ctx * <optional>

Optional content in which to invoke the listener.

Details
Since Source
3.0.0 Component.js, line 146
Inherited From:
Examples
// Listen for all "afterCreate" events in a DataStore
store.on('afterCreate', (mapperName, props, opts, result) => {
  console.log(mapperName) // "post"
  console.log(props.id) // undefined
  console.log(result.id) // 1234
})
store.create('post', { title: 'Modeling your data' }).then((post) => {
  console.log(post.id) // 1234
})
// Listen for the "add" event on a collection
collection.on('add', (records) => {
  console.log(records) // [...]
})
// Listen for "change" events on a record
post.on('change', (record, changes) => {
  console.log(changes) // { changed: { title: 'Modeling your data' } }
})
post.title = 'Modeling your data'

validate(target)

This adds ES5 getters/setters to the target based on the "properties" in this Schema, which makes possible change tracking and validation on property assignment.

Method parameters:
Name Type Description
target Object

The prototype to which to apply this schema.

Details
Source
Schema.js, line 1043

validate(value, opts)

Validate the provided value against this schema.

Method parameters:
Name Type Argument Description
value *

Value to validate.

opts Object <optional>

Configuration options.

Return value:
Type Description
Array | undefined

Array of errors or undefined if valid.

Details
Source
Schema.js, line 1068