Skip to content
This repository was archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
feat(schema): migrate to json schemas using ajv
Browse files Browse the repository at this point in the history
  • Loading branch information
philippspo committed Apr 18, 2016
1 parent 461cf61 commit 2755808
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ notifications:
email: false
node_js:
- 4.3.1
- 0.12

This comment has been minimized.

Copy link
@butenkor

butenkor Apr 18, 2016

Will it install both 4.3 and 0.12 on travis? Which version will be used for test execution?

This comment has been minimized.

before_install:
- npm i -g npm@^2.0.0
before_script:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
]
},
"dependencies": {
"ajv": "4.0.0",

This comment has been minimized.

Copy link
@butenkor

butenkor Apr 18, 2016

Just curious - why did you switch to ajv?

This comment has been minimized.

Copy link
@PhilippSpo

PhilippSpo Apr 18, 2016

Contributor

because of node 0.12 support

"highland": "2.7.1",
"joi": "8.0.4",
"lodash": "4.6.1",
"sphere-node-sdk": "1.10.1",
"underscore": "1.8.3",
Expand Down
12 changes: 9 additions & 3 deletions src/customer-import.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SphereClient } from 'sphere-node-sdk'
import { generatePassword } from './utils'
import Joi from 'joi'
import schema from './schema'
import Promise from 'bluebird'
import _ from 'lodash'
import initAjv from 'ajv'

const validate = Promise.promisify(Joi.validate)
const ajv = initAjv({ removeAdditional: true, coerceTypes: true })
const validate = ajv.compile(schema)

export default class CustomerImport {

Expand Down Expand Up @@ -127,6 +128,11 @@ export default class CustomerImport {
}

validateCustomer (customer) {
return validate(customer, schema, { stripUnknown: true, convert: true })
const isValid = validate(customer)
if (isValid) {
return Promise.resolve()
} else {
return Promise.reject(validate.errors)
}
}
}
41 changes: 25 additions & 16 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import Joi from 'joi'

const schema = Joi.object().keys({
customerNumber: Joi.string(),
companyName: Joi.string(),
addresses: Joi.array().items(Joi.object().keys({
streetName: Joi.string(),
postalCode: Joi.number(),
city: Joi.string(),
country: Joi.string().min(2).max(2)
})),
email: Joi.string().required(),
phone: Joi.string(),
customerGroup: Joi.string(),
vatId: Joi.string()
})
const schema = {

This comment has been minimized.

Copy link
@butenkor

butenkor Apr 18, 2016

Now that you use the same schema structure like here https://github.com/sphereio/sphere-json-schemas/tree/master/schema it would be better to move and maintain it there, right?

This comment has been minimized.

Copy link
@PhilippSpo

PhilippSpo Apr 18, 2016

Contributor

yes it would be.

This comment has been minimized.

Copy link
@PhilippSpo

PhilippSpo Apr 18, 2016

Contributor

I dont really understand why that is a private repo though

This comment has been minimized.

Copy link
@butenkor

butenkor Apr 18, 2016

@hajoeichler shall we make it public?

type: 'object',
properties: {
customerNumber: { type: 'string' },
companyName: { type: 'string' },
addresses: {
type: 'array',
items: {
type: 'object',
properties: {
streetName: { type: 'string' },
postalCode: { type: 'string' },
city: { type: 'string' },
country: { type: 'string', minLength: 2, maxLength: 2 },
}
},
},
email: { type: 'string' },
phone: { type: 'string' },
customerGroup: { type: 'string' },
vatId: { type: 'string' },
},
required: ['email'],
additionalProperties: false,
}

export default schema

0 comments on commit 2755808

Please sign in to comment.