2020-06-19 08:43:22 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import Joi from '@hapi/joi';
|
|
|
|
import {AbstractControl} from '@angular/forms';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ValidationService {
|
|
|
|
|
|
|
|
private vUsername = Joi.string()
|
|
|
|
.lowercase()
|
|
|
|
.pattern(new RegExp('^[a-z0-9-_.]+$'))
|
|
|
|
.min(1)
|
|
|
|
.max(128);
|
|
|
|
|
|
|
|
private vPassword = Joi.string()
|
2020-08-07 10:49:18 +02:00
|
|
|
.min(8)
|
2020-06-19 08:43:22 +02:00
|
|
|
.max(128);
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
generate(method, args) { // generate a Validator function
|
|
|
|
return (control: AbstractControl): {[key: string]: any} | null => {
|
|
|
|
let ok;
|
|
|
|
let error;
|
|
|
|
if (args) {
|
|
|
|
({ok, error} = this[method](control.value, ...args));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
({ok, error} = this[method](control.value));
|
|
|
|
}
|
|
|
|
return ok ? null : { failure: error };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
username(data) {
|
|
|
|
const {ignore, error} = this.vUsername.validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: 'username must only contain a-z0-9-_.'};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
password(data) {
|
|
|
|
const {ignore, error} = this.vPassword.validate(data);
|
|
|
|
if (error) {
|
2020-08-07 10:49:18 +02:00
|
|
|
return {ok: false, error: 'password must have at least 8 characters'};
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:14:29 +02:00
|
|
|
string(data, option = null) {
|
|
|
|
let validator = Joi.string().max(128).allow('');
|
|
|
|
let errorMsg = 'must contain max 128 characters';
|
|
|
|
if (option === 'alphanum') {
|
|
|
|
validator = validator.alphanum();
|
|
|
|
errorMsg = 'must contain max 128 alphanumerical characters';
|
|
|
|
}
|
|
|
|
const {ignore, error} = validator.validate(data);
|
2020-06-19 08:43:22 +02:00
|
|
|
if (error) {
|
2020-07-29 13:14:29 +02:00
|
|
|
return {ok: false, error: errorMsg};
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
stringOf(data, list) {
|
|
|
|
const {ignore, error} = Joi.string().allow('').valid(...list.map(e => e.toString())).validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: 'must be one of ' + list.join(', ')};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
stringLength(data, length) {
|
|
|
|
const {ignore, error} = Joi.string().max(length).allow('').validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: 'must contain max ' + length + ' characters'};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
minMax(data, min, max) {
|
|
|
|
const {ignore, error} = Joi.number().allow('').min(min).max(max).validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: `must be between ${min} and ${max}`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
min(data, min) {
|
|
|
|
const {ignore, error} = Joi.number().allow('').min(min).validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: `must not be below ${min}`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
max(data, max) {
|
2020-06-22 10:22:45 +02:00
|
|
|
const {ignore, error} = Joi.number().allow('').max(max).validate(data);
|
2020-06-19 08:43:22 +02:00
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: `must not be above ${max}`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:42:02 +02:00
|
|
|
url(data) {
|
|
|
|
const {ignore, error} = Joi.string().uri().validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: `must be a valid URL`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:43:22 +02:00
|
|
|
unique(data, list) {
|
|
|
|
const {ignore, error} = Joi.string().allow('').invalid(...list.map(e => e.toString())).validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: `values must be unique`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
2020-07-27 17:52:03 +02:00
|
|
|
|
2020-07-29 13:14:29 +02:00
|
|
|
equal(data, compare) {
|
|
|
|
if (data !== compare) {
|
|
|
|
return {ok: false, error: `must be equal`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
2020-07-27 17:52:03 +02:00
|
|
|
parameterName(data) {
|
|
|
|
const {ignore, error} = Joi.string()
|
|
|
|
.max(128)
|
|
|
|
.invalid('condition_template', 'material_template')
|
2020-08-10 14:28:17 +02:00
|
|
|
.allow('')
|
2020-07-27 17:52:03 +02:00
|
|
|
.pattern(/^[^.]+$/)
|
|
|
|
.required()
|
|
|
|
.messages({'string.pattern.base': 'name must not contain a dot'})
|
|
|
|
.validate(data);
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: error.details[0].message};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
|
|
|
|
parameterRange(data) {
|
|
|
|
if (data) {
|
|
|
|
try {
|
|
|
|
const {ignore, error} = Joi.object({
|
|
|
|
values: Joi.array()
|
|
|
|
.min(1),
|
|
|
|
|
|
|
|
min: Joi.number(),
|
|
|
|
|
|
|
|
max: Joi.number(),
|
|
|
|
|
|
|
|
type: Joi.string()
|
2020-08-24 15:24:55 +02:00
|
|
|
.valid('string', 'number', 'boolean', 'array'),
|
|
|
|
|
|
|
|
required: Joi.boolean()
|
2020-07-27 17:52:03 +02:00
|
|
|
})
|
|
|
|
.oxor('values', 'min')
|
|
|
|
.oxor('values', 'max')
|
|
|
|
.required()
|
|
|
|
.validate(JSON.parse(data));
|
|
|
|
if (error) {
|
|
|
|
return {ok: false, error: error.details[0].message};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return {ok: false, error: `no valid JSON`};
|
|
|
|
}
|
|
|
|
return {ok: true, error: ''};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {ok: false, error: `no valid value`};
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|