Skip to content

Commit

Permalink
Merge pull request #100 from sebelga/feature/#89-typescript-definitions
Browse files Browse the repository at this point in the history
feat(Typescript): Add Typescript support
  • Loading branch information
sebelga authored Apr 11, 2018
2 parents ea5326e + 7135642 commit b80290a
Show file tree
Hide file tree
Showing 9 changed files with 9,051 additions and 3,480 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

[**Documentation**](#documentation) |
[**Example**](#example) |
[**Demo App**](#demo-app) |
[**Demo Application**](#demo-app) |
[**Support**](../../issues) |
[**Changelog**](../../releases)

Expand All @@ -25,7 +25,8 @@ It is not a replacement of @google-cloud/datastore but a layer on top of it to h
- pre & post **middleware** (hooks)
- **custom methods** on entity instances
- **[Joi](https://github.com/hapijs/joi)** schema definition and validation
- :tada: **NEW** Advanced **[cache layer](https://sebelga.gitbooks.io/gstore-node/content/cache.html)** (since v3.0.0)
- **NEW** Advanced **[cache layer](https://sebelga.gitbooks.io/gstore-node/content/cache.html)** (since v3.0.0)
- :tada: **NEW** **[Typescript support](https://sebelga.gitbooks.io/gstore-node/content/typescript.html)** (since v4.1.0)

This library is in active development, please report any issue you might find.

Expand Down Expand Up @@ -117,13 +118,13 @@ const validateAccessList = (value, validator) => {
* Create the schema for the User Model
*/
const userSchema = new Schema({
firstname: { type: 'string', required: true },
lastname: { type: 'string', optional: true },
email: { type: 'string', validate: 'isEmail', required: true },
password: { type: 'string', read: false, required: true },
createdOn: { type: 'string', default: gstore.defaultValues.NOW, write: false, read: false },
dateOfBirth: { type: 'datetime' },
bio: { type: 'string', excludeFromIndexes: true },
firstname: { type: String, required: true },
lastname: { type: String, optional: true },
email: { type: String, validate: 'isEmail', required: true },
password: { type: String, read: false, required: true },
createdOn: { type: String, default: gstore.defaultValues.NOW, write: false, read: false },
dateOfBirth: { type: Date },
bio: { type: String, excludeFromIndexes: true },
website: { validate: 'isURL', optional: true },
ip: {
validate: {
Expand Down
24 changes: 17 additions & 7 deletions lib/helpers/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ const errorToObject = error => (
const validatePropType = (value, propType, prop) => {
let valid;
let ref;
let type = propType;
if (typeof propType === 'function') {
type = propType.name.toLowerCase();
}

switch (propType) {
switch (type) {
case 'string':
/* eslint valid-typeof: "off" */
valid = typeof value === 'string';
ref = 'string.base';
break;
case 'date':
case 'datetime':
valid = isValidDate(value);
ref = 'datetime.base';
Expand All @@ -59,6 +64,7 @@ const validatePropType = (value, propType, prop) => {
valid = is.array(value);
ref = 'array.base';
break;
case 'number':
case 'int': {
const isIntInstance = value.constructor.name === 'Int';
if (isIntInstance) {
Expand Down Expand Up @@ -97,7 +103,11 @@ const validatePropType = (value, propType, prop) => {
}
default:
/* eslint valid-typeof: "off" */
valid = typeof value === propType;
if (Array.isArray(value)) {
valid = false;
} else {
valid = typeof value === type;
}
ref = 'prop.type';
}

Expand Down Expand Up @@ -205,7 +215,7 @@ const validate = (entityData, schema, entityKind) => {
}

if (!skip) {
// ... is allowed
// ... is allowed?
if (!schemaHasProperty) {
error = new gstoreErrors.ValidationError(
gstoreErrors.errorCodes.ERR_PROP_NOT_ALLOWED,
Expand All @@ -220,7 +230,7 @@ const validate = (entityData, schema, entityKind) => {
// return;
}

// ...is required
// ...is required?
isRequired = schemaHasProperty &&
{}.hasOwnProperty.call(schema.paths[prop], 'required') &&
schema.paths[prop].required === true;
Expand All @@ -238,7 +248,7 @@ const validate = (entityData, schema, entityKind) => {
errors.push(errorToObject(error));
}

// ... valid prop Type
// ... is valid prop Type?
if (schemaHasProperty && !isEmpty && {}.hasOwnProperty.call(schema.paths[prop], 'type')) {
error = validatePropType(propertyValue, propertyType, prop);

Expand All @@ -247,7 +257,7 @@ const validate = (entityData, schema, entityKind) => {
}
}

// ... valid prop Value
// ... is valid prop Value?
if (error === null &&
schemaHasProperty &&
!isEmpty &&
Expand All @@ -258,7 +268,7 @@ const validate = (entityData, schema, entityKind) => {
}
}

// ... value in range
// ... is value in range?
if (schemaHasProperty && !isEmpty &&
{}.hasOwnProperty.call(schema.paths[prop], 'values') &&
schema.paths[prop].values.indexOf(propertyValue) < 0) {
Expand Down
Loading

0 comments on commit b80290a

Please sign in to comment.