joyent-portal/consoles/my-joy-images/src/state/validators.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-03-01 03:15:16 +02:00
import intercept from 'apr-intercept';
import keys from 'lodash.keys';
import reduce from 'apr-reduce';
import assign from 'lodash.assign';
import yup from 'yup';
/*****************************************************************************/
const validateField = async (field, value) => {
const [err] = await intercept(field.validate(value));
return err ? err.errors.shift() : '';
};
const validateSchema = async (schema, value) => {
const errors = await reduce(
keys(schema),
async (errors, name) => {
const msg = await validateField(schema[name], value[name]);
return !msg ? errors : assign(errors, { [name]: msg });
},
2018-03-01 03:15:16 +02:00
{}
);
if (keys(errors).length) {
throw errors;
}
2018-03-01 03:15:16 +02:00
};
/*****************************************************************************/
const matches = {
nameStart: /^[a-zA-Z]|\d/,
2018-03-07 17:36:32 +02:00
nameBody: /^([a-zA-Z]|\d|_|-)+$/
2018-03-01 03:15:16 +02:00
};
const msgs = {
required: prefix => `${prefix} must be defined.`,
nameStart: prefix => `${prefix} can only start with letters and numbers.`,
nameBody: prefix =>
2018-03-07 17:36:32 +02:00
`${prefix} cannot contain spaces and can only contain letters, numbers, underscores (_), and hyphens (-).`
2018-03-01 03:15:16 +02:00
};
const Schemas = {
tag: {
name: yup
.string()
.required(msgs.required('Key'))
.matches(matches.nameStart, msgs.nameStart('Key'))
.matches(matches.nameBody, msgs.nameBody('Key')),
value: yup
.string()
.required(msgs.required('Value'))
.matches(matches.nameStart, msgs.nameStart('Value'))
.matches(matches.nameBody, msgs.nameBody('Value'))
},
instanceName: {
name: yup
.string()
.matches(matches.nameStart, msgs.nameStart('Instance name'))
.matches(matches.nameBody, msgs.nameBody('Instance Name'))
}
};
/*****************************************************************************/
export const addTag = tag => {
console.log(tag);
validateSchema(Schemas.tag, tag);
};
2018-03-01 03:15:16 +02:00
export const instanceName = ({ name }) =>
!name ? null : validateSchema(Schemas.instanceName, { name });