Applicative

beginner3 of 7

Learn about applicative in fp-ts validation

Code Editor

03-applicative.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('Applicative validation', () => {
  it('validates correct user', () => {
    const result = validateUser('Alice', 'alice@example.com', 25)
    expect(result._tag).toBe('Right')
    if (result._tag === 'Right') {
      expect(result.right).toEqual({ name: 'Alice', email: 'alice@example.com', age: 25 })
    }
  })

  it('accumulates all validation errors', () => {
    const result = validateUser('A', 'invalid', 16)
    expect(result._tag).toBe('Left')
    if (result._tag === 'Left') {
      expect(result.left.length).toBe(3) // All three validations failed
      expect(result.left).toContain('Name too short')
      expect(result.left).toContain('Invalid email')
      expect(result.left).toContain('Must be 18 or older')
    }
  })

  it('accumulates partial errors', () => {
    const result = validateUser('Alice', 'invalid', 25)
    expect(result._tag).toBe('Left')
    if (result._tag === 'Left') {
      expect(result.left.length).toBe(1)
    }
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises