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 927
Example

// Normally you would do:  import {Schema} from 'js-data'
const JSData = require('js-data@3.0.0-rc.4')
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> ANY_OPS

Validation keywords validated for any type:

  • enum
  • type
  • allOf
  • anyOf
  • oneOf
  • not
Details
Type Source
Array.<String> Schema.js, line 652

<static> ARRAY_OPS

Validation keywords validated for array types:

  • items
  • maxItems
  • minItems
  • uniqueItems
Details
Type Source
Array.<String> Schema.js, line 667

<static> NUMERIC_OPS

Validation keywords validated for numeric (number and integer) types:

  • multipleOf
  • maximum
  • minimum
Details
Type Source
Array.<String> Schema.js, line 680

<static> OBJECT_OPS

Validation keywords validated for object types:

  • maxProperties
  • minProperties
  • required
  • properties
  • dependencies
Details
Type Source
Array.<String> Schema.js, line 692

<static> STRING_OPS

Validation keywords validated for string types:

  • maxLength
  • minLength
  • pattern
Details
Type Source
Array.<String> Schema.js, line 706

<static> typeGroupValidators

A map of validation functions grouped by type.

Details
Type Source
Object Schema.js, line 807

<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

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-rc.4')
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 1253
Example

// Normally you would do: import {Schema} from 'js-data'
const JSData = require('js-data@3.0.0-rc.4')
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 726

apply(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 980

applyDefaults(target)

Apply default values to the target object for missing values.

Method parameters:
Name Type Description
target Object

The target to which to apply values for missing values.

Details
Source
Schema.js, line 1005

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 124
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 202
Inherited From:
Example

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

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

const store = new DataStore()
store.on('beep', function (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 134
Inherited From:

makeDescriptor(prop, schema, opts)

Assemble a property descriptor for tracking and validating changes to a property according to the given schema. This method is called when Mapper#applySchema is set to true.

Method parameters:
Name Type Argument Description
prop String

The property name.

schema Schema | Object

The schema for the property.

opts Object <optional>

Optional configuration.

Properties
Name Type Argument Description
getter Function <optional>

Custom getter function.

setter Function <optional>

Custom setter function.

track Function <optional>

Whether to track changes.

Return value:
Type Description
Object

A property descriptor for the given schema.

Details
Source
Schema.js, line 1040

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 180
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 147
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'

pick(value)

Create a copy of the given value that contains only the properties defined in this schema.

Method parameters:
Name Type Description
value *

The value to copy.

Return value:
Type Description
*

The copy.

Details
Source
Schema.js, line 1184

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 1229