Practical

intermediate6 of 6

Learn about practical in fp-ts these

Code Editor

06-practical.exercise.ts

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

Test Results

Requirements

describe('These practical validation', () => {
  it('validates valid age', () => {
    const result = validateAge(25)
    expect(result).toEqual(Th.right(25))
  })

  it('warns for young age', () => {
    const result = validateAge(15)
    expect(Th.isBoth(result)).toBe(true)
  })

  it('fails for negative age', () => {
    const result = validateAge(-5)
    expect(Th.isLeft(result)).toBe(true)
  })

  it('validates valid email', () => {
    const result = validateEmail('user@example.com')
    expect(result).toEqual(Th.right('user@example.com'))
  })

  it('fails for invalid email', () => {
    const result = validateEmail('invalid')
    expect(Th.isLeft(result)).toBe(true)
  })

  it('warns for short email', () => {
    const result = validateEmail('a@b.c')
    expect(Th.isBoth(result)).toBe(true)
  })

  it('combines successful validations', () => {
    const v1 = validateAge(25)
    const v2 = validateEmail('user@example.com')
    const result = combineValidations(v1, v2)
    expect(result).toEqual(Th.right([25, 'user@example.com']))
  })

  it('accumulates warnings', () => {
    const v1 = validateAge(15)
    const v2 = validateEmail('a@b.c')
    const result = combineValidations(v1, v2)
    expect(Th.isBoth(result)).toBe(true)
    if (Th.isBoth(result)) {
      expect(result.left.length).toBeGreaterThan(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