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

invalid date for authority #37

Closed
mkoo opened this issue May 24, 2015 · 4 comments
Closed

invalid date for authority #37

mkoo opened this issue May 24, 2015 · 4 comments

Comments

@mkoo
Copy link
Member

mkoo commented May 24, 2015

There are instances where we have to address taxonomic rules, e.g., see Alligator: Daudin 1802 '1801' which is the only valid way of citing the authority. So need to remove the validation rules for this field or allow exceptions.

@tigerhawkvok
Copy link
Contributor

Do they have a uniform format? E.g., are they always of form YYYY 'YYYY'?

@mkoo
Copy link
Member Author

mkoo commented May 24, 2015

Yes. but probably " is used more than single.
thx

On Sat, May 23, 2015 at 9:19 PM, Philip Kahn notifications@github.com
wrote:

Do they have a uniform format? E.g., are they always of form YYYY 'YYYY'?


Reply to this email directly or view it on GitHub
#37 (comment)
.

@tigerhawkvok
Copy link
Contributor

Then I'll match it against this regex: ^\d{4}$|^\d{4} (\"|')\d{4}(\"|')$

@tigerhawkvok
Copy link
Contributor

Will change single quotes to double quotes, checks both years, and single year if just the one

testAuthorityYear = (authYearDeepInputSelector) ->
  ###
  # Helper function!
  # Take in a deep element selector, then run it through match
  # patterns for the authority year.
  #
  # @param authYearDeepInputSelector -- Selector for a shadow DOM
  #          element, ideally a paper-input.
  ###
  yearString = d$(authYearDeepInputSelector).val()
  error = undefined
  linnaeusYear = 1707 # Linnaeus's birth year
  d = new Date()
  nextYear = d.getUTCFullYear() + 1 # So we can honestly say "between" and mean it
  # Authority date regex
  # From
  # https://github.com/tigerhawkvok/SSAR-species-database/issues/37
  authorityRegex = /^\d{4}$|^\d{4} (\"|')\d{4}\1$/
  unless isNumber(yearString) and linnaeusYear < yearString < nextYear
    unless authorityRegex.test(yearString)
      # It's definitely bad, we just need to decide how bad
      if yearString.search(" ") is -1
        error = "This must be a valid year between #{linnaeusYear} and #{nextYear}"
      else
        error = "Nonstandard years must be of the form: YYYY 'YYYY', eg, 1801 '1802'"
    else
      # It matches the regex, but fails the check
      # So, it may be valid, but we need to check
      if yearString.search(" ") is -1
        # It's a simple year, but fails the check.
        # Therefore, it's out of range.
        error = "This must be a valid year between #{linnaeusYear} and #{nextYear}"
      else
        # There's a space, so it's of format:
        #   1801 '1802'
        # So we need to parse that out for a valid year check
        # The format is otherwise assured by the regex
        years = yearString.split(" ")
        unless linnaeusYear < years[0] < nextYear
          error = "The first year must be a valid year between #{linnaeusYear} and #{nextYear}"
        altYear = years[1].replace(/(\"|')/g,"")
        unless linnaeusYear < altYear < nextYear
          error = "The second year must be a valid year between #{linnaeusYear} and #{nextYear}"
        # Now, for input consistency, replace single-quotes with
        # double-quotes
        yearString = yearString.replace(/\"/g,'"')
  # If there were any error strings assigned, display an error.      
  if error?
    escapeCompletion = true
    console.warn("#{authYearDeepInputSelector} failed its validity checks for #{yearString}!")
    # Populate the paper-input errors
    try
      $("html /deep/ #{authYearDeepInputSelector} /deep/ paper-input-decorator")
      .attr("error",error)
      .attr("isinvalid","isinvalid")
    catch e
      $("html >>> #{authYearDeepInputSelector} >>> paper-input-decorator")
      .attr("error",error)
      .attr("isinvalid","isinvalid")
  # Return the value for assignment
  return yearString

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants