Skip to content

Commit

Permalink
Add State/Statemodel validation for TextAnnotation
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Aug 25, 2023
1 parent 1701e63 commit d1a66fd
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions pkg/pdfcpu/validate/annotation.go
Expand Up @@ -250,35 +250,32 @@ func validateAnnotationDictText(xRefTable *model.XRefTable, d types.Dict, dictNa
}

// State, optional, text string, since V1.5
// NOTE: acceptable state values depend on the StateModel. for now, accept the union of all valid values for State.
// once the StateModel is loaded and validated, we will check the State again to make sure it corresponds to
// the model.
validate := func(s string) bool {
return types.MemberOf(s, []string{"None", "Marked", "Unmarked", "Completed", "Accepted", "Rejected", "Cancelled"})
state, err := validateStringEntry(xRefTable, d, dictName, "State", OPTIONAL, model.V15, nil)
if err != nil {
return err
}
state, err := validateStringEntry(xRefTable, d, dictName, "State", OPTIONAL, model.V15, validate)

// StateModel, text string, since V1.5
validate := func(s string) bool { return types.MemberOf(s, []string{"Marked", "Review"}) }
stateModel, err := validateStringEntry(xRefTable, d, dictName, "StateModel", state != nil, model.V15, validate)
if err != nil {
return err
}

if state != nil {
// StateModel, text string, since V1.5
validate = func(s string) bool { return types.MemberOf(s, []string{"Marked", "Review"}) }
stateModel, err := validateStringEntry(xRefTable, d, dictName, "StateModel", REQUIRED, model.V15, validate)
if err != nil {
return err
if state == nil {
if stateModel != nil {
return errors.Errorf("pdfcpu: validateAnnotationDictText: dict=%s missing state for statemodel=%s", dictName, *stateModel)
}
return nil
}

// Ensure that the state/model combo is valid.
var validStates []string
if *stateModel == "Marked" {
validStates = []string{"Marked", "Unmarked"}
} else {
validStates = []string{"Accepted", "Rejected", "Cancelled", "Completed", "None"}
}
if !types.MemberOf(*state, validStates) {
return errors.Errorf("pdfcpu: validateAnnotationDictText: dict=%s invalid state=%s for state model=%s", dictName, *state, *stateModel)
}
// Ensure that the state/model combo is valid.
validStates := []string{"Accepted", "Rejected", "Cancelled", "Completed", "None"} // stateModel "Review"
if *stateModel == "Marked" {
validStates = []string{"Marked", "Unmarked"}
}
if !types.MemberOf(*state, validStates) {
return errors.Errorf("pdfcpu: validateAnnotationDictText: dict=%s invalid state=%s for state model=%s", dictName, *state, *stateModel)
}

return nil
Expand Down

0 comments on commit d1a66fd

Please sign in to comment.