Пример #1
0
export default createObjectPrototype({
    implements: [ITextAreaField],
    extends: [BaseField],
    
    constructor: function (options) {
        this._IBaseField.constructor.call(this, options)

        if (options) {
            this._minLength = options.minLength
            delete options.minLength
            this._maxLength = options.maxLength
            delete options.maxLength
        }
    },
    
    validate: function (inp) {
        var error = this._IBaseField.validate.call(this, inp)
        if (error) { return error }

        if (!this._isRequired && (inp === null || typeof inp === "undefined" || inp === '')) {
            return
        }        

        if (inp && typeof inp !== "string") {
            return {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--text_area_field_no_string', 'The field doesn\'t contain text'),
                message: "Fältet är inte en sträng"
            }
        } else if (this._minLength && inp.length < this._minLength) {
            return {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--text_field_too_short', 'The text is too short. Min ${minLength} chars.'),
                message: "Texten är för kort. Minst " + this._minLength + " tkn"
            }
        } else if (this._maxLength && inp.length > this._maxLength) {
            return {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--text_field_too_long', 'The text is too long. Max ${maxLength} chars.'),
                message: "Texten är för lång. Max " + this._maxLength + " tkn"
            }
        }
    }
})
Пример #2
0
export default createObjectPrototype({
    implements: [IMultiSelectField],

    extends: [BaseField],
    
    constructor: function (options) {
        this._IBaseField.constructor.call(this, options)
        
        if (options) {
            // Always set valueType to required to get validation per item
            // Should be an instance of a field.
            this.valueType = options.valueType; 
            this.options = options.options;            
        }
        
    },
    
    validate: function (inp, options, context, async) {
        // Check that this isn't undefined if it is required
        var error = this._IBaseField.validate.call(this, inp)
        if (error) { return error }
        
        // If undefined and not required just return ok
        if (typeof inp === 'undefined' || inp === null) {
            return
        }
        
        // We need to check every item in the list to see that they are valid
        var errors = inp.map(function (item) {
            // Chack value is of valueType
            var error = this.valueType.validate(item)
            if (typeof error !== 'undefined') {
                if (!aync) {
                    return error
                } else {
                    return Promise.resolve(error)
                }
            }

            var options = this.options

            // Check that selected value is in options list
            var matches = false
            for (var i = 0; i < options.length; i++) {
                if (options[i].name === item) {
                    matches = true
                    break
                }
            }
            if (!matches) {
                return {
                    type: 'constraint_error',
                    i18nLabel: i18n('isomorphic-schema--multi_select_field_value_error', 'One or more of the selected values is not allowed'),
                    message: "Ett eller flera valda värden finns inte i listan över tillåtna värden"
                }
            }
        }.bind(this)).filter(function (item) {return typeof item !== 'undefined'})
        if (!async || errors.length === 0) {
            return errors[0]
        } else {
            return Promise.all(errors)
                .then(function (results) {
                    var results = results.filter(function (item) { item !== undefined })
                    return Promise.resolve(results[0])
                })
        }
    },

    toFormattedString: function (inp) {
        return inp
    },

    fromString: function (inp) {
        return inp
    },

    getOptionTitle: function (name) {
        for (var i=0; i < this.options.length; i++) {
            if (this.options[i].name === name) return this.options[i].title 
        }
    }
})
export default createObjectPrototype({
    implements: [IDynamicSelectAsyncBaseField],

    extends: [DynamicSelectBaseField],
    
    constructor: function (options) {
        this._IDynamicSelectBaseField.constructor.call(this, options)
    },

    // DynamicSelectAsyncBaseField:
    validateAsync: function (inp, options, context) {
        // Check that this isn't undefined if it is required
        var error = this._IBaseField.validate.call(this, inp)
        if (error) { return Promise.resolve(error) };    
    
        // Since we have passed the required test we can just check if the value is undefined
        // or null and return field errors undefined 
        if (inp === null || typeof inp === 'undefined') {
            return Promise.resolve(undefined)
        }

        if (inp) {
            return this.valueType.validateAsync(inp, options, context)
        } else {
            return Promise.resolve(undefined)
        }

        /*

        Example implementation:

        var result = this._IDynamicSelectAsyncBaseField.validateAsync.call(this, inp)

        // Check if we failed validation in DynamicSelectAsyncBaseField
        if (result) {
            return result
        }

        return doAsyncCall(inp)
            .then(function (didMatch) {
                if (didMatch) {
                    return Promise.resolve(undefined)
                } else {
                    return Promise.resolve({
                        type: 'constraint_error',
                        i18nLabel: i18n('isomorphic-schema--select_field_value_error', 'The selected value is not allowed'),
                        message: "Valt värde finns inte i listan över tillåtna värden"
                    })
                }
            })
            .catch(function (err) {
                throw new Error('Couldn\t perform validation: ' + err.message)
            })
        */
    },

    validate: function () {
        throw new Error("You tried to call a field extending DynamicSelectAsyncBaseField synchronously, needs to be async. Use .validateAsync(...) instead!")
    },

    // This is used by formlibs
    getOptionsAsync: function (inp, options, context) {
        /*
        return doAsyncCall(inp)
            .then(function (results) {
                var outp = results.map(function (item) {
                    return { name: item.id, title: item.title }
                })
                return Promise.resolve(outp)
            })
            .catch(function (err) {
                throw new Error('Couldn\t fetch options: ' + err.message)
            })
        */
    },

    getOptions: function () {
        throw new Error("You tried to call a field extending DynamicSelectAsyncBaseField synchronously, needs to be async. Use .getOptionsAsync(...) instead!")
    },

    // This is used by formlibs
    getOptionTitleAsync: function (inp, options, context) {
        /*
        return doAsyncCall(inp)
            .then(function (title) {
                if (title) {
                    return Promise.resolve(title)
                } else {
                    return Promise.resolve({
                        type: 'value_error',
                        i18nLabel: i18n('isomorphic-schema--select_field_value_error', 'The selected value is not allowed'),
                        message: "Valt värde finns inte i listan över tillåtna värden"
                    })
                }
            })
            .catch(function (err) {
                throw new Error('Couldn\t fetch title: ' + err.message)
            })
        */
    },

    getOptionTitle: function () {
        throw new Error("You tried to call a field extending DynamicSelectAsyncBaseField synchronously, needs to be async. Use .getOptionTitleAsync(...) instead!")
    },

    toFormattedString: function (inp) {
        return this.valueType.fromString(inp)
    },

    fromString: function (inp) {
        return this.valueType.fromString(inp)
    }
})
Пример #4
0
'use strict';
var createObjectPrototype = require('component-registry').createObjectPrototype;
var IWorkflow = require('../interfaces').IWorkflow;

var Workflow = createObjectPrototype({
    implements: [IWorkflow],
    
    constructor: function (params) {
        if (!this.hasOwnProperty('_workflows')) {
            this._workflows = {};
        }
    }
});

module.exports = Workflow;
Пример #5
0
/*
    Password-field
*/
import { IPasswordField } from '../interfaces'

export default createObjectPrototype({
    implements: [IPasswordField],

    extends: [TextField],
    
    constructor: function (options) {
        this._ITextField.constructor.call(this, options)
    },
    
    validate: function (inp) {
        var error = this._ITextField.validate.call(this, inp)
        if (error) { return error }
    
        if(inp !== null && typeof inp !== 'undefined' && inp.length < 8) {
            error = {
                type: 'constraint_error',
                i18nLabel: i18n('isomorphic-schema--password_field_too_short', 'The password must contain at least 8 chars'),
                message: "Lösenordet måste innehålla minst 8 tecken"
            }
        
            return error
        }
    
    }
})
Пример #6
0
export default createObjectPrototype({
    implements: [IListField],

    extends: [BaseField],
    
    constructor: function (options) {
        this._IBaseField.constructor.call(this, options)
        
        if (options) {
            // Always set valueType to required to get validation per item
            // Should be an instance of a field.
            this.valueType = options.valueType || options.valueFieldType
            this.options = options.options
            this._minItems = options.minItems
            delete options.minItems
            this._maxItems = options.maxItems
            delete options.maxItems
        }
        
    },
    
    validate: function (inp, options, context, async) {
        if (inp && inp.length == 0) { inp = undefined }
        var error = this._IBaseField.validate.call(this, inp)
        if (error) { return error }
        
        // If it is undefined or null, then we just return 
        // that this value is ok
        if (!inp) {
            return undefined
        }
        
        // Check that this is an array
        if (!(inp instanceof Array)) {
            error = {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--list_field_type_error', 'This is not a proper list. This is a bug in the application'),
                message: "This is not a proper list. This is a bug in the application"
            }
            //console.log(error)
            return error;            
        }
        
        if (this._maxItems && inp.length > this._maxItems) {
            error = {
                type: 'value_error',
                i18nLabel: i18n('isomorphic-schema--list_field_value_error_too_many_items', 'Too many items in list, max ${maxItems} allowed'),
                message: 'Too many items in list, max ${maxItems} allowed'
            }
        }
    
        if (this._minItems && inp.length < this._minItems) {
            error = {
                type: 'value_error',
                i18nLabel: i18n('isomorphic-schema--list_field_value_error_too_few_items', 'Too few items in list, min ${minItems} allowed'),
                message: 'Too few items in list, min ${minItems} allowed'
            }
        }

        var tmpValidationErrors = []
        var validationPromises = []
        inp.forEach(function (item, i) {
            // Make sure we have a new options object...
            if (!options) {
                var newOptions = {}
            } else {
                var newOptions = cloneShallow(options)
            }
            
            // ...so we can add objectPath to determine where we are
            if (Array.isArray(newOptions.objectPath)) {
                newOptions.objectPath = newOptions.objectPath.concat([i])
            } else {
                newOptions.objectPath = [i]
            }

            if (!async) {
                var tmpError = this.valueType.validate(item, newOptions, context)
                if (tmpError) {
                    tmpValidationErrors.push({id: i, error: tmpError})
                }
            } else {
                var promise = this.valueType.validateAsync(item, newOptions, context)
                // Do not return this promise, add it to validationPromises. We will 
                // handle the list of validationPromises further down
                if (promise && promise.then) {
                    validationPromises.push(
                        promise.then(function (tmpError) {
                            if (tmpError) {
                                return Promise.resolve({id: i, error: tmpError})
                            }
                        })
                    )
                }
            }
        }.bind(this))
    
        if (!async || validationPromises.length === 0) {
            if (tmpValidationErrors.length > 0) {
                var tmpErrors = {}
                tmpValidationErrors.map(function (item) {
                    tmpErrors[item.id] = item.error
                })
            
                if (error !== undefined) {
                    error.errors = tmpErrors
                } else {
                    error = {
                        type: 'list_error',
                        i18nLabel: i18n('isomorphic-schema--list_field_value_error', 'There is an error in the content of this list'),
                        message: "There is an error in the content of this list",
                        errors: tmpErrors
                    }
                }
            }
            // If there weren't any errors this will return undefined
            return error
        } else {
            return Promise.all(validationPromises)
                .then(function (errors) {
                    // Filter out non-errors (undefined)
                    errors = errors.filter(function (error) { return error })
                    if (errors.length === 0) {
                        return Promise.resolve(undefined)
                    } else {
                        var tmpErrors = {}
                        errors.forEach(function (err) {
                            tmpErrors[err.id] = err.error
                        })
                        return Promise.resolve({
                            type: 'list_error',
                            i18nLabel: i18n('isomorphic-schema--list_field_value_error', 'There is an error in the content of this list'),
                            message: "There is an error in the content of this list",
                            errors: tmpErrors
                        })
                    }
                })
        }
        
    },
    
    toFormattedString: function (inp) {
        return inp
    },
    
    fromString: function (inp) {
        var _this = this
        if ((inp instanceof Array)) {
            return inp.map(function (item) {
                return _this.valueType.fromString(item)
            })
        } else {
            return inp
        }
    }
})
Пример #7
0
export default createObjectPrototype({
    implements: [IIntegerField],

    extends: [BaseField],
    
    constructor: function (options) {
        this._IBaseField.constructor.call(this, options)
        if (options) {
            this._minValue = options.min
            delete options.min
            this._maxValue = options.max
            delete options.max;            
        }
    },
    
    validate: function (inp) {
        var error = this._IBaseField.validate.call(this, inp)
        if (error) { return error }

        if (!this._isRequired && (inp === null || typeof inp === "undefined" || inp === '')) {
            return
        }

        if (typeof inp !== "number" || isNaN(inp)) {
            return {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--integer_field_not_number', 'The field doesn\'t contain numbers'),
                message: "Värdet innehåller annat än siffror"
            }
        }

        if (parseInt(inp) !== inp) {
            return {
                type: 'type_error',
                i18nLabel: i18n('isomorphic-schema--integer_field_no_decimals', 'The field may not contain decimals'),
                message: "Värdet får inte innehålla en decimaldel"
            }
        }

        if (typeof this._minValue !== 'undefined' && inp < this._minValue) {
            return {
                type: 'constraint_error',
                i18nLabel: i18n('isomorphic-schema--integer_field_too_small', 'The value is too small. Min ${minValue}'),
                message: "Minimum " + this._minValue
            }
        }

        if (typeof this._maxValue !== 'undefined' && inp > this._maxValue) {
            return {
                type: 'constraint_error',
                i18nLabel: i18n('isomorphic-schema--integer_field_too_big', 'The value is too big. Max ${maxValue}'),
                message: "Max " + this._maxValue
            }
        }
    },
    
    toFormattedString: function (inp) {
        return (typeof inp === 'undefined' || inp === null ? '' : inp + '')
    },
    
    fromString: function (inp) {
        if (typeof inp === "string") {
            var tmp = parseInt(inp.replace(reInteger, ""))
        } else {
            var tmp = parseInt(inp)
        }
        if (isNaN(tmp)) {
            return undefined
        } else {
            return tmp
        }
    }
})