Skip to content

Commit

Permalink
Adopt standard code style
Browse files Browse the repository at this point in the history
  • Loading branch information
qzb committed Jun 6, 2016
1 parent be2c880 commit f3f253a
Show file tree
Hide file tree
Showing 24 changed files with 1,842 additions and 1,818 deletions.
98 changes: 49 additions & 49 deletions lib/fields/array.js
Expand Up @@ -12,68 +12,68 @@ const Field = require('./field')
* field.deserialize([ 'foo', 'bar' ]) // throws validation error
*/
class ArrayField extends Field {
/**
* @param {object} field - field which will be used to serialize/deserialize array's elements
* @param {object} [params]
* @param {boolean} [params.required]
* @param {array} [params.default]
* @param {number} [params.minLength] - minimum length of array
* @param {number} [params.maxLength] - maximum length of array
*/
constructor(field, params) {
super(field, params)
}

init(field, params) {
super.init(params)
/**
* @param {object} field - field which will be used to serialize/deserialize array's elements
* @param {object} [params]
* @param {boolean} [params.required]
* @param {array} [params.default]
* @param {number} [params.minLength] - minimum length of array
* @param {number} [params.maxLength] - maximum length of array
*/
constructor (field, params) { // eslint-disable-line no-useless-constructor
super(field, params)
}

if (!field) {
throw new Error('Field is missing')
}
init (field, params) {
super.init(params)

this.field = field
if (!field) {
throw new Error('Field is missing')
}

deserialize(value, options) {
if (Array.isArray(value) === false) {
throw new Field.ValidationError('Value must be an array')
}
this.field = field
}

if (this.params.minLength !== undefined && value.length < this.params.minLength) {
throw new Field.ValidationError(`Value must have at least ${this.params.minLength} elements`)
}

if (this.params.maxLength !== undefined && value.length > this.params.maxLength) {
throw new Field.ValidationError(`Value must have at most ${this.params.maxLength} elements`)
}
deserialize (value, options) {
if (Array.isArray(value) === false) {
throw new Field.ValidationError('Value must be an array')
}

const result = []
const errors = {}
let errorOccurred = false
if (this.params.minLength !== undefined && value.length < this.params.minLength) {
throw new Field.ValidationError(`Value must have at least ${this.params.minLength} elements`)
}

for (let i = 0; i < value.length; ++i) {
try {
result.push(this.field.deserialize(value[i], options))
} catch (error) {
if (!(error instanceof Field.ValidationError)) {
throw error
}
if (this.params.maxLength !== undefined && value.length > this.params.maxLength) {
throw new Field.ValidationError(`Value must have at most ${this.params.maxLength} elements`)
}

errorOccurred = true
errors[i] = error.message
}
}
const result = []
const errors = {}
let errorOccurred = false

if (errorOccurred) {
throw new Field.ValidationError(errors)
for (let i = 0; i < value.length; ++i) {
try {
result.push(this.field.deserialize(value[i], options))
} catch (error) {
if (!(error instanceof Field.ValidationError)) {
throw error
}

return result
errorOccurred = true
errors[i] = error.message
}
}

serialize(value, options) {
return value.map(e => this.field.serialize(e, options))
if (errorOccurred) {
throw new Field.ValidationError(errors)
}

return result
}

serialize (value, options) {
return value.map(e => this.field.serialize(e, options))
}
}

module.exports = ArrayField;
module.exports = ArrayField
46 changes: 23 additions & 23 deletions lib/fields/boolean.js
Expand Up @@ -13,32 +13,32 @@ const Field = require('./field')
* field.deserialize('123') // throws validation error
*/
class BooleanField extends Field {
/**
* @param {object} [params]
* @param {boolean} [params.required]
* @param {boolean} [params.default]
*/
constructor(params) {
super(params)
}

deserialize(value) {
if (typeof value === 'boolean') {
return value
}
/**
* @param {object} [params]
* @param {boolean} [params.required]
* @param {boolean} [params.default]
*/
constructor (params) { // eslint-disable-line no-useless-constructor
super(params)
}

if (typeof value === 'string') {
if (value.match(/^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$/)) {
return true
}
deserialize (value) {
if (typeof value === 'boolean') {
return value
}

if (value.match(/^(n|N|no|No|NO|false|False|FALSE|off|Off|OFF)$/)) {
return false
}
}
if (typeof value === 'string') {
if (value.match(/^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$/)) {
return true
}

throw new Field.ValidationError('Value must be a boolean')
if (value.match(/^(n|N|no|No|NO|false|False|FALSE|off|Off|OFF)$/)) {
return false
}
}

throw new Field.ValidationError('Value must be a boolean')
}
}

module.exports = BooleanField;
module.exports = BooleanField
48 changes: 24 additions & 24 deletions lib/fields/email.js
Expand Up @@ -14,34 +14,34 @@ const StringField = require('./string')
* field.deserialize('SOME.EMAIL.GMAIL.COM') // throws validation error
*/
class EmailField extends StringField {
/**
* @param {object} [params]
* @param {boolean} [params.caseSensitive] - disables normalization to lower case
* @param {boolean} [params.required]
* @param {string} [params.default]
*/
constructor(params) {
super(params)
/**
* @param {object} [params]
* @param {boolean} [params.caseSensitive] - disables normalization to lower case
* @param {boolean} [params.required]
* @param {string} [params.default]
*/
constructor (params) { // eslint-disable-line no-useless-constructor
super(params)
}

init (params) {
super.init(params)
this.params.trim = true
}

deserialize (value) {
value = super.deserialize(value)

if (!this.params.caseSensitive) {
value = value.toLowerCase()
}

init(params) {
super.init(params)
this.params.trim = true
if (value && !validateEmail(value)) {
throw new Field.ValidationError('Value must be a valid email address')
}

deserialize(value) {
value = super.deserialize(value)

if (!this.params.caseSensitive) {
value = value.toLowerCase()
}

if (value && !validateEmail(value)) {
throw new Field.ValidationError('Value must be a valid email address')
}

return value
}
return value
}
}

module.exports = EmailField
96 changes: 48 additions & 48 deletions lib/fields/enum.js
Expand Up @@ -13,67 +13,67 @@ const Field = require('./field')
* field.deserialize('six') // throws validation error
*/
class EnumField extends Field {
/**
* @param {string[]} choices - list of possible choices
* @param {object} [params]
* @param {boolean} [params.caseSensitive] - makes validation case sensitive
* @param {boolean} [params.required]
* @param {string} [params.default]
*/
constructor(choices, params) {
super(choices, params)
/**
* @param {string[]} choices - list of possible choices
* @param {object} [params]
* @param {boolean} [params.caseSensitive] - makes validation case sensitive
* @param {boolean} [params.required]
* @param {string} [params.default]
*/
constructor (choices, params) { // eslint-disable-line no-useless-constructor
super(choices, params)
}

init (choices, params) {
super.init(params)

if (!choices) {
throw new Error('Choices list is missing')
}

init(choices, params) {
super.init(params)

if (!choices) {
throw new Error('Choices list is missing')
}

if (!choices.length) {
throw new Error('Choices list cannot be empty')
}

if (choices.find(c => typeof c !== 'string')) {
throw new Error('All choices must be a strings')
}
if (!choices.length) {
throw new Error('Choices list cannot be empty')
}

const choicesMap = {}
if (choices.find(c => typeof c !== 'string')) {
throw new Error('All choices must be a strings')
}

for (const choice of choices) {
if (this.params.caseSensitive) {
choicesMap[choice.trim()] = choice
} else {
choicesMap[choice.trim().toLowerCase()] = choice
}
}
const choicesMap = {}

if (Object.keys(choicesMap).length !== choices.length) {
throw new Error('Choices cannot be doubled')
}
for (const choice of choices) {
if (this.params.caseSensitive) {
choicesMap[choice.trim()] = choice
} else {
choicesMap[choice.trim().toLowerCase()] = choice
}
}

if (this.params.default !== undefined && choices.indexOf(this.params.default) === -1) {
throw new Error('Default value must be one of choices')
}
if (Object.keys(choicesMap).length !== choices.length) {
throw new Error('Choices cannot be doubled')
}

this.choices = choices
this.choicesMap = choicesMap
if (this.params.default !== undefined && choices.indexOf(this.params.default) === -1) {
throw new Error('Default value must be one of choices')
}

deserialize(value) {
value = value.trim()
this.choices = choices
this.choicesMap = choicesMap
}

if (!this.params.caseSensitive) {
value = value.toLowerCase()
}
deserialize (value) {
value = value.trim()

if (!this.choicesMap.hasOwnProperty(value)) {
throw new Field.ValidationError('Value must be one of allowed choices: ' + this.choices.join(', '))
}
if (!this.params.caseSensitive) {
value = value.toLowerCase()
}

return this.choicesMap[value]
if (!this.choicesMap.hasOwnProperty(value)) {
throw new Field.ValidationError(`Value must be one of allowed choices: ${this.choices.join(', ')}`)
}

return this.choicesMap[value]
}
}

module.exports = EnumField

0 comments on commit f3f253a

Please sign in to comment.