Skip to content

Commit

Permalink
Stable Version 1.0.0-alpha.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Nov 2, 2014
1 parent ce53d01 commit 2744b98
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 338 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##### 1.0.0-alpha.1 - 01 November 2014

Stable Version 1.0.0-alpha.1

##### 0.1.0 - 15 September 2014

Initial Release
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing Guide

First, feel free to contact me with questions. [Mailing List](https://groups.google.com/forum/?fromgroups#!forum/js-data-project). [Issues](https://github.com/js-data/js-data-schema/issues).
First, feel free to contact me with questions. [Mailing List](https://groups.io/org/groupsio/jsdata). [Issues](https://github.com/js-data/js-data-schema/issues).

1. Contribute to the issue that is the reason you'll be developing in the first place
1. Fork js-data-schema
Expand Down
39 changes: 27 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = function (grunt) {
test: 'test'
};

var pkg = grunt.file.readJSON('package.json');

grunt.initConfig({
config: config,
pkg: grunt.file.readJSON('package.json'),
Expand Down Expand Up @@ -66,17 +68,6 @@ module.exports = function (grunt) {
}
},

groc: {
javascript: [
'dist/js-data-schema.js'
],
options: {
'out': 'doc/',
// 'index': 'doc.md',
'repository-url': 'https://github.com/js-data/js-data-schema'
}
},

browserify: {
dist: {
options: {
Expand Down Expand Up @@ -110,13 +101,37 @@ module.exports = function (grunt) {
}
});

grunt.registerTask('banner', function () {
var file = grunt.file.read('dist/js-data-schema.js');

var banner = '/**\n' +
'* @author Jason Dobry <[email protected]>\n' +
'* @file js-data-schema.js\n' +
'* @version ' + pkg.version + ' - Homepage <http://www.js-data.io/docs/js-data-schema>\n' +
'* @copyright (c) 2014 Jason Dobry \n' +
'* @license MIT <https://github.com/js-data/js-data-schema/blob/master/LICENSE>\n' +
'*\n' +
'* @overview Define and validate rules, datatypes and schemata in Node and in the browser.\n' +
'*/\n';

file = banner + file;

grunt.file.write('dist/js-data-schema.js', file);
});

grunt.registerTask('test', [
'build',
'jshint:test',
'mochaTest',
'karma:dist'
]);
grunt.registerTask('build', ['clean', 'jshint:src', 'browserify', 'uglify']);
grunt.registerTask('build', [
'clean',
'jshint:src',
'browserify',
'banner',
'uglify'
]);
grunt.registerTask('go', ['build', 'watch']);

grunt.registerTask('default', ['build']);
Expand Down
296 changes: 1 addition & 295 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,301 +65,7 @@ errors; // {

## API

### Schemator

#### Schemator()
```js
var schemator = new Schemator();
```

#### Schemator#availableDataTypes()
```js
schemator.availableDataTypes(); // ['boolean', 'string', 'etc.']
```

#### Schemator#availableRules()
```js
schemator.availableRules(); // ['type', 'minLength', 'etc.']
```

#### Schemator#availableSchemata()
```js
schemator.defineSchema('PersonSchema', { ... });
schemator.availableSchemata(); // ['PersonSchema']
```

#### Schemator#getDataType(name)
```js
schemator.getDataType('myDataType');
```

#### Schemator#getRule(name)
```js
schemator.getRule('myRule');
```

#### Schemator#getSchema(name)
```js
schemator.getSchema('PersonSchema');
```

#### Schemator#removeDataType(name)
```js
schemator.removeDataType('myDataType');
```

#### Schemator#removeRule(name)
```js
schemator.removeRule('myRule');
```

#### Schemator#removeSchema(name)
```js
schemator.removeSchema('PersonSchema');
```

#### Schemator#defineDataType(name, typeDefinition)
```js
schemator.defineDataType('NaN', function (x) {
if (isNaN(x)) {
return null;
} else {
return {
rule: 'type',
actual: typeof x,
expected: 'NaN'
};
}
});
```

#### Schemator#defineRule(name, ruleFunc[, async])
```js
schemator.defineRule('divisibleBy', function (x, divisor) {
if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
return {
rule: 'divisibleBy',
actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
expected: '' + x + ' % ' + divisor + ' === 0'
};
}
return null;
});

schemator.defineSchema('mySchema', {
seats: {
divisibleBy: 4
}
});

var errors = schemator.getSchema('mySchema').validateSync({
seats: 16
});

errors; // null

errors = schemator.getSchema('mySchema').validateSync({
seats: 17
});

errors; // {
// seats: {
// errors: [ {
// rule: 'divisibleBy',
// actual: '17 % 4 === 1',
// expected: '17 % 4 === 0'
// } ]
// }
// }
```

Asynchronous rule:
```js
schemator.defineRule('divisibleBy', function (x, divisor, cb) {

// asynchronity here is fake, but you could do something async, like make an http request
setTimeout(function () {
if (typeof x === 'number' && typeof divisor === 'number' && x % divisor !== 0) {
cb({
rule: 'divisibleBy',
actual: '' + x + ' % ' + divisor + ' === ' + (x % divisor),
expected: '' + x + ' % ' + divisor + ' === 0'
});
}
cb(null);
}, 1);
}, true); // pass true as the third argument

schemator.defineSchema('mySchema', {
seats: {
divisibleBy: 4
}
});

var errors = schemator.getSchema('mySchema').validate({
seats: 16
}, function (err) {
errors; // null

errors = schemator.getSchema('mySchema').validate({
seats: 17
}, function (err) {
errors; // {
// seats: {
// errors: [ {
// rule: 'divisibleBy',
// actual: '17 % 4 === 1',
// expected: '17 % 4 === 0'
// } ]
// }
// }
});
});
```

#### Schemator#defineSchema(name, definition)
```js
schemator.defineSchema('PersonSchema', {
name: {
first: {
type: 'string',
maxLength: 255
},
last: {
type: 'string',
maxLength: 255
}
},
age: {
type: 'number',
max: 150,
min: 0
}
});
```

#### Schemator#validate(schemaName, attrs[, options], cb)
See `Schema#validate(attrs[, options], cb)`

#### Schemator#validateSync(schemaName, attrs[, options])
See `Schema#validateSync(attrs[, options])`

#### Schemator#setDefaults(schemaName, attrs)
See `Schema#setDefaults(attrs)`

#### Schemator#getDefaults()
See `Schema#getDefaults()`

#### Schemator#addDefaultsToTarget(schemaName, target[, overwrite])
See `Schema#addDefaultsToTarget(target)`

#### Schemator#stripNonSchemaAttrs(schemaName, target)
See `Schema#stripNonSchemaAttrs(target)`

### Schema

#### Schema#validate(attrs[, options], cb)
```js
PersonSchema.validate({
name: 'John Anderson'
}, function (err) {
err; // null
});

PersonSchema.validate({
name: 5
}, function (err) {
err; // {
// name: {
// errors: [{
// rule: 'type',
// actual: 'number',
// expected: 'string'
// }]
// }
// }
});
```

#### Schema#validateSync(attrs[, options])
```js
var errors = PersonSchema.validate({
name: 'John Anderson'
});

errors; // null

errors = mySchema.validate({
name: 5
});
errors; // {
// name: {
// errors: [{
// rule: 'type',
// actual: 'number',
// expected: 'string'
// }]
// }
// }
```

#### Schema#setDefaults(attrs)
```js
PersonSchema.setDefaults({
first: '',
last: '',
plan: 'free'
});
```

#### Schema#getDefaults()
```js
PersonSchema.getDefaults(); // {
first: '',
last: '',
age: 0
}
```

#### Schema#addDefaultsToTarget(target[, overwrite])
```js
var person = {
first: 'John',
plan: 'premium'
};

PersonSchema.addDefaultsToTarget(person);

person; // {
first: 'John',
last: '',
plan: 'premium'
}

PersonSchema.addDefaultsToTarget(person, true);

person; // {
first: '',
last: '',
plan: 'free'
}
```

#### Schema#stripNonSchemaAttrs(target)
```js
var person = {
first: 'John',
plan: 'premium',
nonSchema: 'value'
};

PersonSchema.stripNonSchemaAttrs(person);

person; // {
first: 'John',
plan: 'premium'
}
```
[js-data-schema api](http://www.js-data.io/docs/js-data-schema)

## License
[MIT License](https://github.com/js-data/js-data-schema/blob/master/LICENSE)
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"author": "Jason Dobry",
"name": "js-data-schema",
"description": "Define and validate rules, datatypes and schemata in Node and in the browser.",
"version": "0.1.0",
"homepage": "https://github.com/js-data/js-data-schema",
"version": "1.0.0-alpha.1",
"homepage": "http://www.js-data.io/docs/js-data-schema",
"repository": {
"type": "git",
"url": "https://github.com/js-data/js-data-schema.git"
Expand Down
Loading

0 comments on commit 2744b98

Please sign in to comment.