Problem: Build a Simple Validation Rule Builder

Easy
15 min
Construct basic input validation rules using a fluent, chainable API.

Problem statement

Your team is developing a form validation system for user inputs, including email addresses, names, and passwords. Currently, each validation rule is manually configured using raw objects, resulting in inconsistent formatting and missing constraints. You’ve been asked to build a fluent RuleBuilder to define a field’s validation rules more clearly.

Goal

Create a RuleBuilder class with the following chainable methods:

  • .required(): Marks the field as required.

  • .minLength(n: number): Sets minimum string length.

  • .maxLength(n: number): Sets maximum string length.

  • .build(): Returns an object with the final shape: { required?: true, minLength?: number, maxLength?: number }.

Each method is optional. If none are called before .build(), return an empty object.

Constraints

  • Do not include fields that were never explicitly set.

  • Do not use defaults or preset values.

  • Return a plain object, not a class instance.

Sample output

The examples below illustrate what the output should look like:

// Required and max length
const rules1 = new RuleBuilder()
.required()
.maxLength(20)
.build();
console.log(rules1);
/* Expected output:
{ required: true, maxLength: 20 } */
// Only min length
const rules2 = new RuleBuilder()
.minLength(5)
.build();
console.log(rules2);
/* Expected output:
{ minLength: 5 } */
// No rules
const rules3 = new RuleBuilder().build();
console.log(rules3);
/* Expected output:
{} */
// All rules
const rules4 = new RuleBuilder()
.required()
.minLength(3)
.maxLength(15)
.build();
console.log(rules4);
/* Expected output:
{ required: true, minLength: 3, maxLength: 15 } */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Build a Simple Validation Rule Builder

Easy
15 min
Construct basic input validation rules using a fluent, chainable API.

Problem statement

Your team is developing a form validation system for user inputs, including email addresses, names, and passwords. Currently, each validation rule is manually configured using raw objects, resulting in inconsistent formatting and missing constraints. You’ve been asked to build a fluent RuleBuilder to define a field’s validation rules more clearly.

Goal

Create a RuleBuilder class with the following chainable methods:

  • .required(): Marks the field as required.

  • .minLength(n: number): Sets minimum string length.

  • .maxLength(n: number): Sets maximum string length.

  • .build(): Returns an object with the final shape: { required?: true, minLength?: number, maxLength?: number }.

Each method is optional. If none are called before .build(), return an empty object.

Constraints

  • Do not include fields that were never explicitly set.

  • Do not use defaults or preset values.

  • Return a plain object, not a class instance.

Sample output

The examples below illustrate what the output should look like:

// Required and max length
const rules1 = new RuleBuilder()
.required()
.maxLength(20)
.build();
console.log(rules1);
/* Expected output:
{ required: true, maxLength: 20 } */
// Only min length
const rules2 = new RuleBuilder()
.minLength(5)
.build();
console.log(rules2);
/* Expected output:
{ minLength: 5 } */
// No rules
const rules3 = new RuleBuilder().build();
console.log(rules3);
/* Expected output:
{} */
// All rules
const rules4 = new RuleBuilder()
.required()
.minLength(3)
.maxLength(15)
.build();
console.log(rules4);
/* Expected output:
{ required: true, minLength: 3, maxLength: 15 } */

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
class RuleBuilder {
// implement methods here
}
// Example usage:
// Required and max length
const rules1 = new RuleBuilder()
.required()
.maxLength(20)
.build();
console.log(rules1);
/* Expected output:
{ required: true, maxLength: 20 } */
// Only min length
const rules2 = new RuleBuilder()
.minLength(5)
.build();
console.log(rules2);
/* Expected output:
{ minLength: 5 } */
// No rules
const rules3 = new RuleBuilder().build();
console.log(rules3);
/* Expected output:
{} */
// All rules
const rules4 = new RuleBuilder()
.required()
.minLength(3)
.maxLength(15)
.build();
console.log(rules4);
/* Expected output:
{ required: true, minLength: 3, maxLength: 15 } */