Skip to content

Commit

Permalink
Schema First Design (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prefinem committed Sep 24, 2018
1 parent b83db0c commit ade6e15
Show file tree
Hide file tree
Showing 34 changed files with 631 additions and 486 deletions.
67 changes: 48 additions & 19 deletions README.md
Expand Up @@ -12,36 +12,55 @@

![Dependencies](https://david-dm.org/Prefinem/nativemodels.svg) ![Dev Dependencies](https://david-dm.org/Prefinem/nativemodels/dev-status.svg)

Native Models provides a way to map objects and arrays of objects in a clean and typed way. The main goal is to ensure runtime type checking and consistent models for APIs.
Native Models provides a way to map objects in a clean and typed way. The main goal is to ensure runtime type checking and consistent models for APIs.

## Getting Started

```js
import { arrayModel, objectModel } from 'nativemodels';
import { boolean, computed, date, int, string } from 'nativemodels/datatypes';
import { createModel } from 'nativemodels';
import { array, boolean, computed, date, int, object, string } from 'nativemodels/datatypes';

const schema = {
const photoSchema = {
ext: string(),
url: string().required(),
};

const contactSchema = {
email: string(),
phone: string(),
url: string(),
};

const userSchema = {
accountID: int().nullable(),
contact: object(contactSchema),
created: date(),
firstName: string().required(),
lastName: string().required(),
fullName: computed((record) => `${record.firstName} ${record.lastName}`),
isAdmin: boolean().nullable(),
lastName: string().required(),
photos: array(object(photoSchema)),
typeID: int().default(2),
isAdmin: boolean().default(false),
accountID: int().nullable(),
created: date().default(new Date()),
updated: date().default(new Date()),
};

const userModel = objectModel(schema);
const userModel = createModel(userSchema);

const johnSmith = userModel({
contact: {
email: 'j.smith@example.com',
},
firstName: 'John',
lastName: 'Smith',
photos: [
{
ext: '.jpg',
url: 'https://example.com/img.jpg',
},
],
});
// => { firstName: 'John', lastName: 'Smith', fullName: 'John Smith', ...}

const usersModel = arrayModel(schema);

const users = usersModel([
const userRecords = [
{
firstName: 'John',
lastName: 'Smith',
Expand All @@ -50,7 +69,9 @@ const users = usersModel([
firstName: 'Jane',
lastName: 'Doe',
},
]);
];
const users = userRecords.map(userModel);
// => [{ firstName: 'John', lastName: 'Smith', fullName: 'John Smith', ...}]

const janeDoe = userModel({
...johnSmith,
Expand All @@ -62,7 +83,7 @@ const janeDoe = userModel({

## Datatype API

Datatype methods that can be called or extended.
Datatype methods that can be chained when defining schema.

### datatypes.default(defaultValue)

Expand All @@ -72,19 +93,27 @@ Sets a default value if no value is set

Allows the value set to be null (useful for database models)

### datatypes.parse(value)

Parses the value being set. Used to extend base datatype

### datatypes.required()

Forces the value to be required. Is ignored if default value is set

## Datatypes

- array
- boolean
- computed
- date
- float
- int
- object
- string

## Extending Datatypes

### datatypes.validate(value, name)

If value is valid, returns true, else throws error. Name is key on object;

### datatypes.parse(value)

Parses the value being set. Used to extend base datatype
50 changes: 0 additions & 50 deletions dist/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
"prettify": "prettier --config ./.prettierrc.json --write \"src/**\"",
"pub": "./publish.sh",
"staged": "lint-staged",
"test": "yarn build && jest --coverage"
"test": "yarn build && cp -R tests/ tests-dist/ && jest --coverage"
},
"version": "0.0.6"
}
4 changes: 0 additions & 4 deletions publish.sh
Expand Up @@ -2,9 +2,5 @@ yarn build
cp ./package.json ./dist/package.json
cp ./README.md ./dist/README.md
cd dist
npm version patch
npm publish
cp ./package.json ./../package.json
cd ./../
git add package.json
git commit -m "Updated package.json to latest version"
9 changes: 0 additions & 9 deletions src/arrayModel.js

This file was deleted.

File renamed without changes.
18 changes: 18 additions & 0 deletions src/datatypes/array.js
@@ -0,0 +1,18 @@
const base = require('./base');
const parseValue = require('./../lib/parseValue');

const array = (type) => ({
...base,
parse(values, name) {
return values.map((value) => parseValue(type, name, value));
},
validate(value, name) {
if (Array.isArray(value)) {
return true;
}

throw new Error(`Property ${name} is not an array`);
},
});

module.exports = array;
3 changes: 3 additions & 0 deletions src/datatypes/base.js
Expand Up @@ -18,6 +18,9 @@ const base = {

return this;
},
validate() {
return true;
},
};

module.exports = base;
4 changes: 2 additions & 2 deletions src/datatypes/date.js
Expand Up @@ -2,9 +2,9 @@ const base = require('./base');

const date = () => ({
...base,
parse(value, name) {
validate(value, name) {
if (value instanceof Date) {
return value;
return true;
}

throw new Error(`Property ${name} is not a date`);
Expand Down
14 changes: 7 additions & 7 deletions src/datatypes/float.js
@@ -1,13 +1,13 @@
const base = require('./base');

const float = () => ({
...base,
parse(value, name) {
if (value === true || value === false || value === '') {
throw new Error(`Property ${name} is not a float`);
}

if (!isNaN(parseFloat(value))) {
return parseFloat(value);
parse(value) {
return parseFloat(value);
},
validate(value, name) {
if (!isNaN(parseFloat(value)) && value !== true && value !== false && value !== '') {
return true;
}

throw new Error(`Property ${name} is not a float`);
Expand Down
4 changes: 4 additions & 0 deletions src/datatypes/index.js
@@ -1,17 +1,21 @@
const array = require('./array');
const base = require('./base');
const boolean = require('./boolean');
const computed = require('./computed');
const date = require('./date');
const float = require('./float');
const int = require('./int');
const object = require('./object');
const string = require('./string');

module.exports = {
array,
base,
boolean,
computed,
date,
float,
int,
object,
string,
};
9 changes: 6 additions & 3 deletions src/datatypes/int.js
Expand Up @@ -2,18 +2,21 @@ const base = require('./base');

const int = () => ({
...base,
parse(value, name) {
parse(value) {
return parseInt(value);
},
validate(value, name) {
if (
value !== true &&
value !== false &&
value !== '' &&
!isNaN(parseInt(value)) &&
parseInt(value) === parseFloat(value)
) {
return parseInt(value);
return true;
}

throw new Error(`Property ${name} is not a int`);
throw new Error(`Property ${name} is not an int`);
},
});

Expand Down
18 changes: 18 additions & 0 deletions src/datatypes/object.js
@@ -0,0 +1,18 @@
const base = require('./base');
const createModel = require('./../createModel');

const object = (schema) => ({
...base,
parse(value) {
return createModel(schema)(value);
},
validate(value, name) {
if (typeof value === 'object' && !Array.isArray(value)) {
return true;
}

throw new Error(`Property ${name} is not an object`);
},
});

module.exports = object;
3 changes: 3 additions & 0 deletions src/datatypes/string.js
Expand Up @@ -2,6 +2,9 @@ const base = require('./base');

const string = () => ({
...base,
parse(value) {
return `${value}`;
},
});

module.exports = string;
6 changes: 2 additions & 4 deletions src/index.js
@@ -1,9 +1,7 @@
const arrayModel = require('./arrayModel');
const createModel = require('./createModel');
const datatypes = require('./datatypes');
const objectModel = require('./objectModel');

module.exports = {
arrayModel,
createModel,
datatypes,
objectModel,
};
2 changes: 1 addition & 1 deletion src/lib/parseRecord.js
Expand Up @@ -4,7 +4,7 @@ const parseRecord = (schema, record, defaultedRecord) =>
Object.keys(record).reduce(
(result, key) => ({
...result,
...(schema[key] ? { [key]: parseValue(schema, key, record[key]) } : {}),
...(schema[key] ? { [key]: parseValue(schema[key], key, record[key]) } : {}),
}),
defaultedRecord,
);
Expand Down
10 changes: 7 additions & 3 deletions src/lib/parseValue.js
@@ -1,9 +1,13 @@
const parseValue = (schema, key, value) => {
if (schema[key].allowNull && value === null) {
const parseValue = (type, key, value) => {
if (type.allowNull && value === null) {
return null;
}

return schema[key].parse(value, key);
if (type.validate(value, key)) {
return type.parse(value, key);
}

throw new Error(`Failed to validate ${key} with value of ${value}`);
};

module.exports = parseValue;
2 changes: 1 addition & 1 deletion src/lib/proxyHandler.js
Expand Up @@ -23,7 +23,7 @@ const set = (schema, target, property, value) => {
throw new Error(`${property} is not a property of model`);
}

target[property] = parseValue(schema, property, value);
target[property] = parseValue(schema[property], property, value);

return true;
};
Expand Down

0 comments on commit ade6e15

Please sign in to comment.