Practical Example

intermediate5 of 5

Learn about practical example in fp-ts flow

Code Editor

05-practical-example.exercise.ts

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

Test Results

Requirements

describe('parseUser', () => {
  it('successfully parses valid user data', () => {
    const rawData: RawUserData = {
      email: 'alice@example.com',
      age: '25',
      name: 'Alice'
    }
    const result = parseUser(rawData)
    expect(result).toEqual({
      _tag: 'Right',
      right: { email: 'alice@example.com', age: 25, name: 'Alice' }
    })
  })

  it('rejects invalid email', () => {
    const rawData: RawUserData = {
      email: 'notanemail',
      age: '25',
      name: 'Alice'
    }
    const result = parseUser(rawData)
    expect(result).toEqual({ _tag: 'Left', left: 'Invalid email' })
  })

  it('rejects invalid age', () => {
    const rawData: RawUserData = {
      email: 'alice@example.com',
      age: 'not a number',
      name: 'Alice'
    }
    const result = parseUser(rawData)
    expect(result).toEqual({ _tag: 'Left', left: 'Invalid age' })
  })

  it('rejects too short name', () => {
    const rawData: RawUserData = {
      email: 'alice@example.com',
      age: '25',
      name: 'A'
    }
    const result = parseUser(rawData)
    expect(result).toEqual({ _tag: 'Left', left: 'Name too short' })
  })
})
🧪

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