Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug(farrow-schema): Literal validation in non-strict mode #163

Closed
tqma113 opened this issue Feb 26, 2022 · 0 comments
Closed

Bug(farrow-schema): Literal validation in non-strict mode #163

tqma113 opened this issue Feb 26, 2022 · 0 comments

Comments

@tqma113
Copy link
Collaborator

tqma113 commented Feb 26, 2022

The current implement of Literal validation is

Validator.impl<S.LiteralType>(S.LiteralType, (schema) => ({
  validate: (input, options) => {
    const value = schema.value
    if (input === value) {
      return Ok(input as S.Literals)
    }

    if (options?.strict === false && typeof value !== 'string') {
      if (typeof value === 'number') {
        const result = parseNumberLiteral(input)
        if (result.isOk) return result
      } else if (typeof value === 'boolean') {
        const result = parseBooleanLiteral(input)
        if (result.isOk) return result
      }
    }

    return SchemaErr(`${input} is not a literal ${value}`)
  },
}))

in non-strict mode, after parsing input to number or boolean, if should it compare the value and the parsing result?

Like this

if (options?.strict === false && typeof value !== 'string') {
  if (typeof value === 'number') {
    const result = parseNumberLiteral(input)
    if (result.isOk && result.value === value) return result
  } else if (typeof value === 'boolean') {
    const result = parseBooleanLiteral(input)
    if (result.isOk && result.value === value) return result
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant