Coder Social home page Coder Social logo

rule-validation-api's Introduction

Rule Validation API

Build Status Maintainability Test Coverage

NodeJS Backend Engineer (FullTime)

  • [Node] - HTML enhanced for web apps!
  • [Express] - awesome web-based text editor

The task:

Create a simple rule-validation API.

(You are welcome to use ANY framework or third party library of your choice)

The response structure for your API should be fashioned after the popular JSEND pattern.

Example:

{
  "message": "API response message",
  "status": "success",
  "data": {
    isValidForRule: true,
  }
}

"message" - Apt response message for your API. (Unless otherwise stated, this message can be anything you decide) "status" - Status for your API response. The two possible values for this prop are "success" and "error" (PS: This isn't the response HTTP status code) "data" - This is the meat of your API response. It should contain your actual response data.

Your rule-validation API should have just two routes.

1/ First route is the base route. HTTP GET "/" It should return with data in the following format:

{
  "message": "My Rule-Validation API"
  "status": "success",
  "data": {
    "name": "Amos Burton",
    "github": "@amosburton",
    "email": "[email protected]",
    "mobile": "08069920011",
    "twitter": "@amosb"
  }
}

Please note: a/ name should be your full name b/ github should be your github handle (the '@' symbol must be included) c/ email should be your preferred email of contact d/ mobile should be your preferred mobile number of contact e/ twitter should be your twitter handle (the '@' symbol must be included). [PS: This field is optional, you can choose to include it in your response or not]

2/ Second route is the rule validation route. HTTP POST "/validate-rule" The route should accept JSON data containing a rule and data field to validate the rule against. Example:

{
  "rule": {
    "field": "missions"
    "condition": "gte",
    "condition_value": 30
  },
  "data": {
    "name": "James Holden",
    "crew": "Rocinante",
    "age": 34,
    "position": "Captain",
    "missions": 45
  }
}

Endpoint requirements/constraints: a/ The rule and data fields are required.

b/ The rule field should be a valid JSON object and should contain the following required fields: b1/ field: The field in the data passed to validate the rule against. Your implementation for the field should also support nested data objects. e.g. if field is passed as "card.first6" it means you need to check to see if the data contains a card field, then check to see if the card field contains a first6 field. [PS: The nesting should not be more than two levels] b2/ condition: The condition to use for validating the rule. Accepted condition values are: i/ eq: Means the field value should be equal to the condition value ii/ neq: Means the field value should not be equal to the condition value iii/ gt: Means the field value should be greater than the condition value iv/ gte: Means the field value should be greater than or equal to the condition value v/ contains: Means the field value should contain the condition value b3/ condition_value: The condition value to run the rule against. Your rule evaluation is expected to be like: ["data.field"] ["rule.condition"] ["rule.condition_value"]

c/ The data field can be any of: c1/ A valid JSON object c2/ A valid array c3/ A string

d/ If a required field isn't passed. Your endpoint should return with a response (HTTP 400 status code) that is similar to the below: { "message": "[field] is required." "status": "error", "data": null } E.g. if the rule field is not passed, your endpoint response should be: { "message": "rule is required." "status": "error", "data": null }

e/ If a field is of the wrong type, your endpoint should return with a response (HTTP 400 status code) that is similar to the below: { "message": "[field] should be a|an [type]." "status": "error", "data": null } E.g. if the rule field is passed as a number instead of a valid object, your endpoint response should be: { "message": "rule should be an object." "status": "error", "data": null }

f/ If an invalid JSON payload is passed to your API, your endpoint response (HTTP 400 status code) should be: { "message": "Invalid JSON payload passed." "status": "error", "data": null }

[PS: For the error responses described above, please note that the punctuation is important. I.e the period at the end of each error message.]

g/ If the field specified in the rule object is missing from the data passed, your endpoint response (HTTP 400 status code) should be: { "message": "field [name of field] is missing from data." "status": "error", "data": null }

e.g. { "message": "field age is missing from data." "status": "error", "data": null }

h/ If the rule is successfully validated, your endpoint response (HTTP 200 status code) should be: { "message": "field [name of field] successfully validated." "status": "success", "data": { "validation": { "error": false, "field": "[name of field]", "field_value": [value of field], "condition": "[rule condition]", "condition_value: [condition value] } } }

e.g. { "message": "field missions successfully validated." "status": "success", "data": { "validation": { "error": false, "field": "missions", "field_value": 30, "condition": "gte", "condition_value: 30 } } }

i/ If the rule validation fails, your endpoint response (HTTP 400 status code) should be: { "message": "field [name of field] failed validation." "status": "error", "data": { "validation": { "error": true, "field": "[name of field]", "field_value": [value of field], "condition": "[rule condition]", "condition_value: [condition value] } } }

{ "message": "field missions failed validation." "status": "error", "data": { "validation": { "error": true, "field": "missions", "field_value": 30, "condition": "gte", "condition_value: 54 } } }

j/ Host your solution on Glitch / Heroku or any other platform you prefer. Be sure that the base route and API endpoint routes are accessible. e.g. if your solution is hosted at myflwsoln.herokuapp.com .. the following routes should be accessible a/ HTTP [GET] myflwsoln.herokuapp.com/ b/ HTTP [POST] myflwsoln.herokuapp.com/validate-rule

Once done, submit your solution using this google form https://forms.gle/qnWtpNiCMrfnPuqe9

Example JSON request payloads: = EX1 = { "rule": { "field": "missions.count" "condition": "gte", "condition_value": 30 }, "data": { "name": "James Holden", "crew": "Rocinante", "age": 34, "position": "Captain", "missions": { count: 45, successful: 44, failed: 1 } } }

Response: (HTTP 200) { "message": "field missions.count successfully validated." "status": "success", "data": { "validation": { "error": false, "field": "missions.count", "field_value": 45, "condition": "gte", "condition_value: 30 } } }

= EX2 = { "rule": { "field": "0" "condition": "eq", "condition_value": "a" }, "data": "damien-marley" }

Response: (HTTP 400) { "message": "field 0 failed validation." "status": "error", "data": { "validation": { "error": true, "field": "0", "field_value": "d", "condition": "eq", "condition_value: "a" } } }

= EX3 = { "rule": { "field": "5" "condition": "contains", "condition_value": "rocinante" }, "data": ["The Nauvoo", "The Razorback", "The Roci", "Tycho"] }

Response: (HTTP 400) { "message": "field 5 is missing from data." "status": "error", "data": null }

rule-validation-api's People

Contributors

cc-bezalel avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.