Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from nodes-vapor/feature/convenient-array-vali…
Browse files Browse the repository at this point in the history
…dation

add convenient call to Array type for validating
  • Loading branch information
martinlasek authored Feb 13, 2018
2 parents 5619c5d + 61c408d commit 4b15921
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/*.xcodeproj
Package.pins

Package.resolved
8 changes: 4 additions & 4 deletions Sources/Forms/Fieldset/FieldsetMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import HTTP

public class FieldsetMiddleware: Middleware {
public init() {}

public func respond(
to request: Request,
chainingTo next: Responder
) throws -> Response {
let session = request.session

// move fieldset from session to request
request.fieldset = session?.fieldset
session?.fieldset = nil

let response = try next.respond(to: request)

// store any new fieldset in the session for the next request
session?.fieldset = response.fieldset
return response
Expand Down
2 changes: 1 addition & 1 deletion Sources/Forms/Fieldset/ViewData+fieldSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension ViewData {
}
self = viewData
}

public init(
fieldset: Node? = nil,
request: Request? = nil,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Forms/FieldsetEntryRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public protocol FieldsetEntryRepresentable {
}

extension FieldsetEntryRepresentable {

/// Creates FieldsetEntry value from FormField with given key
public func makeFieldsetEntry() -> FieldsetEntry? {
guard let key = key else {
return nil
}

return FieldsetEntry(
key: key,
label: label,
Expand Down
10 changes: 8 additions & 2 deletions Sources/Forms/Form.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ extension Form {

// MARK: ValidationModeValidatable

extension Form {
extension Array where Element == ValidationModeValidatable {

/// Validates each field according to the validation mode.
/// Throws on first invalid field.
public func validate(inValidationMode mode: ValidationMode) throws {
guard mode != .none else { return }

try fields.forEach { try $0.validate(inValidationMode: mode) }
try forEach { try $0.validate(inValidationMode: mode) }
}
}

extension Form {
public func validate(inValidationMode mode: ValidationMode) throws {
try fields.map { $0 as ValidationModeValidatable }.validate(inValidationMode: mode)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Forms/FormField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension FormField: FieldsetEntryRepresentable {
public var node: NodeRepresentable? {
return value
}

public var errorReasons: [String] {
do {
try validate(value)
Expand Down

0 comments on commit 4b15921

Please sign in to comment.