/** * * * @static * @param {any} topic * @returns * * @memberOf StorePattern */ static find(topic) { return { topic, cmd: 'find', collection: Joi.string().required(), query: Joi.object().required(), options: Joi.object() .keys({ fields: Joi.alternatives().try(Joi.object(), Joi.array()), orderBy: Joi.alternatives().try(Joi.object(), Joi.array(), Joi.string()), offset: Joi.number().integer(), limit: Joi.number().integer() }) .default({}) } }
validate(p){ const schema = Joi.object().keys({ id: Joi.number().integer(), name: Joi.string().min(3).max(30).required(), price: Joi.number().precision(2).required() }); const {error} = Joi.validate(p, schema); if(error){ return error.details[0].message; } return null; }
it('should properly create a GraphQL data type and support int scalar types', async () => { const joiSchema = object().keys({ a: number().integer() }); Joi2GQL.type(joiSchema)._typeConfig.fields.a.type.should.equal( GraphQLInt ); });
/** * Request Validators */ constructor() { super(); this.deleteUser = new JoiValidate({ 'params': { 'schema': Joi.object().keys({ id: Joi.number().integer().min(1).required(), }).unknown(), }, }); }
// schema validation using joi static schema () { return { employeeNumber: joi.number().integer(), firstname: joi.string().alphanum().min(3).max(30).required(), lastname: joi.string().alphanum().min(3).max(30).required(), grade: joi.string().regex(/^[a-zA-Z0-9]{3}$/).required(), birthDate: joi.date().raw().required(), joinDate: joi.date().raw().required(), salary: joi.number().required(), email: joi.string().email() } }
it('should create a GraphQL data type and correctly set the args', async () => { const config = { name: 'Subject', args: { id: number().integer() } }; const expected = { type: typeDictionary.number }; Joi2GQL.type(internals.buildJoiSchema(), config).constructor.should.equal( GraphQLObjectType ); Joi2GQL.type(internals.buildJoiSchema(), config)._typeConfig.args.id.should.deep.equal(expected); });
function editValidate(review) { const schema = { star: Joi.number().integer().min(1).max(5).required(), title: Joi.string().min(3).max(30).required(), details: Joi.string().min(3).max(1024).required() }; return Joi.validate(review, schema); }
it('should properly create a GraphQL data type and support complex required fields', async () => { const joiSchema = object().keys({ a: number().integer().required() }); Joi2GQL.type(joiSchema)._typeConfig.fields.a.type.should.deep.equal( new GraphQLNonNull(GraphQLInt) ); });
server.route({ method: 'GET', path: '/user/{uid}', handler: function (request, reply) { const uid = request.params.uid; connection.query('SELECT uid, username, email FROM users WHERE uid = "' + uid + '"', function (error, results, fields) { if (error) throw error; reply(results); }); }, config: { validate: { params: { uid: Joi.number().integer() } } } });
Joi.object().keys({ id : Joi.number().integer(), firstname : Joi.string().max(128).optional(), lastname : Joi.string().max(128).optional(), created_at : Joi.date().default(() => moment().format(), 'date created'), updated_at : Joi.date().default(() => moment().format(), 'date updated'), })
it('should construct graphql data type given a recurisve felicity constructor', async () => { const typeName = 'Subject'; const config = { name: typeName }; const joiSchema = object().keys({ prop1: string(), prop2: number().integer(), prop3: lazy(() => joiSchema).description(typeName) }); const subject = Joi2GQL.type(joiSchema, config); (subject._typeConfig.fields instanceof Function).should.be.true; subject._typeConfig.fields().prop3.type._typeConfig.name.should.equal(typeName); subject.constructor.should.equal( GraphQLObjectType ); });
buildFieldSchema(fieldConfig) { switch (fieldConfig.type) { case 'integer': { let schema = Joi.number().integer(); if (fieldConfig.min) { schema = schema.min(fieldConfig.min); } if (fieldConfig.max) { schema = schema.max(fieldConfig.max); } if (fieldConfig.required) { schema = schema.required(); } return schema; } case 'select': { return Joi.any().valid(fieldConfig.options); } } }
it('should properly construct a graphql data type given a recursive felicity constructor nested in an array', async () => { const config = { name : 'Subject', args : { id: number().integer() }, resolve: function () {} }; const joiSchema = object().keys({ a: string(), b: number().integer(), c: number(), d: object().keys({ prop1: string(), prop2: string() }), e: boolean(), f: array().items(lazy(() => joiSchema).description('Subject')) // May not need users to specify }); Joi2GQL.type(joiSchema, config).constructor.should.equal( GraphQLObjectType ); });
function validate(review) { const schema = { company: Joi.objectId().required(), star: Joi.number().integer().min(1).max(5).required(), title: Joi.string().min(3).max(30).required(), details: Joi.string().min(3).max(1024).required() }; return Joi.validate(review, schema); }
it('should assign name to Anon if one was not given', async () => { const joiSchema = object().keys({ a: string(), b: number().integer() }); Joi2GQL.type(joiSchema).name.should.equal('Anon'); });