當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript validate.js類代碼示例

本文整理匯總了TypeScript中validate.js的典型用法代碼示例。如果您正苦於以下問題:TypeScript js類的具體用法?TypeScript js怎麽用?TypeScript js使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了js類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: validateRequest

    // res: Response where to send error HTTP message if the request is invalid
    // requestBody: req.body OR req.query
    // constraints: validate.js format
    // return true if the request is valid
    static validateRequest(res: Response,
                           requestBody: any,
                           constraints: any): boolean {
        console.log(requestBody);
        if (_.isEmpty(requestBody)) {
            APIHelper.sendEmptyRequestResponse(res);
            return false;
        }
        let validation = validate(requestBody, constraints);
        console.log(requestBody);
        if (_.keys(validation).length > 0) {
            let validationErrors = {};
            for (let prop in validation) {
                if (!validation.hasOwnProperty(prop)) {
                    continue;
                }
                validationErrors[prop] = validation[prop];
            }
            APIHelper.sendInvalidInputResponse(res, validationErrors);
            return false;
        } else {

            return true;
        }
    }
開發者ID:JohanBaskovec,項目名稱:libraryBackEnd,代碼行數:29,代碼來源:apiHelper.ts

示例2: bookSearchGET

function bookSearchGET(req: Request, res: Response, next: NextFunction) {
    if (Object.keys(req.query).length === 0 && req.baseUrl !== "/api") {
        res.render("book/search", {data: {}});
    }
    else {
        let constraints = {
            title: {
                presence: false,
            },
            isbn: {
                presence: false
            },
            author: {
                presence: false
            },
            pages: {
                presence: false,
                numericality: {
                    onlyInteger: true,
                    greaterThan: 0,
                }
            },
            year: {
                presence: false,
                numericality: {
                    onlyInteger: true,
                }
            },
            editorid: {
                presence: false,
                numericality: {
                    onlyInteger: true,
                }
            }
        };
        let book: Book = new Book();

        let validation = validate(req.query, constraints);
        let data = {
            errors: {global: ""},
            info: {confirmation: ""},
            books: [],
            book: {}
        };
        let wrongInputs = 0;
        for (let prop in validation) {
            if (!validation.hasOwnProperty(prop)) {
                continue;
            }
            data.errors[prop] = validation[prop];
            delete req.query[prop];
            wrongInputs++;
        }
        for (let prop in req.query) {
            if (!req.query.hasOwnProperty(prop)) {
                continue;
            }
            book[prop] = req.query[prop];
        }


        if (wrongInputs === 0) {
            let whereQuery = _.omit(_.pick(req.query, "pages", "year", "editorid"),
                function (value, key, object) {
                    return value === "";
                });
            let likeQuery_ = _.chain(req.query)
                .pick(req.query, "title", "isbn", "author")
                .omit(function (value, key, object) {
                    return value === "";
                }).value();
            let likeQuery = _.mapObject(likeQuery_, function (value, key) {
                return "%" + value + "%";
            });

            console.log("Like query=");
            console.log(likeQuery);
            Book.dao.find({
                where: whereQuery, like: likeQuery
            }, function (err, books) {
                if (err) {
                    data.errors.global = err.code;
                } else {
                    data.books = books;
                    let asyncToDo = data.books.length;

                    _.each(data.books, function (value, index, list) {
                        Editor.dao.find({where: {id: list[index].editorid}}, function (err, editors) {
                            list[index].editor = editors[0];
                            asyncToDo--;
                            if (asyncToDo === 0) {
                                data.info.confirmation = books.length + " books found.";

                                data.book = req.query;

                                if (req.baseUrl === "/api") {
                                    res.json(data);
                                    return;
                                }
                                if (req.xhr) {
//.........這裏部分代碼省略.........
開發者ID:JohanBaskovec,項目名稱:nodejslibrary,代碼行數:101,代碼來源:bookController.ts

示例3: single

const validateField = (validations: any, field?: FieldProps) => {
  if (field) {
    const fieldState = { ...emptyField };

    validations.presence = true;

    fieldState.value = field.value;
    fieldState.validReason = single(field.value, validations);
    fieldState.valid = fieldState.validReason === undefined;

    if (fieldState.valid) {
      fieldState.dirty = false;
    } else {
      fieldState.dirty = field.dirty;
    }

    return fieldState;
  }
};
開發者ID:Itera,項目名稱:ekstern-utlegg,代碼行數:19,代碼來源:personalia.ts

示例4: function

export default function(model) {
  return validate(model, validationSpec);
};
開發者ID:rrevanth,項目名稱:adventures-reactive-web-dev,代碼行數:3,代碼來源:validation.ts

示例5: bookCreateOrUpdate

function bookCreateOrUpdate(req: Request, res: Response,
                            update: boolean) {
    let renderObject = {};
    let currentYear = new Date().getFullYear();
    let constraints = {
        title: {
            presence: true,
        },
        isbn: {
            presence: update ? false : true
        },
        author: {
            presence: true
        },
        pages: {
            presence: true,
            numericality: {
                onlyInteger: true,
                greaterThan: 0,
            }
        },
        year: {
            presence: true,
            numericality: {
                onlyInteger: true,
                lessThan: currentYear,
                notLessThan: "must be in the past."
            }
        },
        editorid: {
            presence: true,
            numericality: {
                onlyInteger: true,
            }
        }
    };
    res.locals.errors = {};
    let book: Book = new Book();

    let validation = validate(req.body, constraints);
    let wrongInputs = 0;
    for (let prop in validation) {
        if (!validation.hasOwnProperty(prop)) {
            continue;
        }
        res.locals.errors[prop] = validation[prop];
        delete req.body[prop];
        wrongInputs++;
    }
    for (let prop in req.body) {
        if (!req.body.hasOwnProperty(prop)) {
            continue;
        }
        book[prop] = req.body[prop];
    }

    res.locals.info = {};
    if (wrongInputs === 0) {

        if (update) {
            Book.dao.update(book, function (err) {
                if (err) {
                    res.locals.errors.global = err;
                } else {
                    res.locals.info.confirmation = "The book has been updated.";
                }
            });
        } else {
            Book.dao.create(book, function (err) {
                if (err) {
                    res.locals.errors.global = err;
                } else {
                    res.locals.info.confirmation = "The book has been created.";
                }
            });
        }
    }
    res.render("book/create", {
        book: book,
        update: update
    });
}
開發者ID:JohanBaskovec,項目名稱:nodejslibrary,代碼行數:82,代碼來源:bookController.ts

示例6: validation

function validation(obj: any, constraint, options?: any ) {
  return validateJs(obj, constraint, options)
}
開發者ID:my-koop,項目名稱:module.utils,代碼行數:3,代碼來源:validation.ts

示例7: function

const updateState = (state: RowsFormProps, action: UpdateRowAction) => {
  const newState = { ...state };

  for (const idx in newState.rows) {
    let row = newState.rows[idx];

    if (row.id === action.row.id) {
      row = { ...row, ...action.row };

      validate.extend(validate.validators.datetime, {
        parse: function(value: any) {
          return +moment.utc(value);
        },
        format: function(value: any, options: any) {
          const format = options.dateOnly
            ? 'YYYY-MM-DD'
            : 'YYYY-MM-DD hh:mm:ss';
          return moment.utc(value).format(format);
        }
      });

      row.validReason = validate(
        row,
        {
          date: {
            presence: {
              message: 'må være en gyldig dato som ikke er i fremtid'
            },
            date: {
              earliest: moment()
                .subtract(1, 'years')
                .format('YYYY-MM-DD'),
              latest: moment().format('YYYY-MM-DD'),
              message: 'må være en gyldig dato som ikke er i fremtid'
            }
          },
          description: {
            presence: { message: 'må være minst 2 tegn' },
            length: { minimum: 2, message: 'må være minst 2 tegn' }
          },
          supplier: {
            presence: { message: 'må være minst 2 tegn' },
            length: { minimum: 2, message: 'må være minst 2 tegn' }
          },
          cost: {
            presence: { message: 'må være + NOK' },
            numericality: { greaterThan: 0, message: 'må være + NOK' }
          }
        },
        { fullMessages: false }
      );

      row.valid = row.validReason === undefined;

      if (row.valid) {
        row.dirty = false;
      }

      newState.rows[idx] = row;
    }

    if (row.valid) {
      const cost = getCost(row);

      // Force to number
      row.cost = cost;
    }
  }

  newState.total = calculateSum(newState.rows);

  return newState;
};
開發者ID:Itera,項目名稱:ekstern-utlegg,代碼行數:73,代碼來源:rows.ts

示例8: editorSearchPartialGET

function editorSearchPartialGET(req: Request, res: Response, next: NextFunction) {
    let data = {
        errors: {global: ""},
        info: {confirmation: ""},
        editors: {},
        // Clone the search form to send it back
        editorSearchForm: req.query,
        layout: !req.xhr
    };

    if (Object.keys(req.query).length === 0) {
        res.render("partials/book/editorSearchDiv", data);
    }
    else {
        // Data to send back to the browser


        // Constraint applied to the query
        let constraints = {
            city: {
                presence: false
            },
            name: {
                presence: false
            },
            id: {
                presence: false,
                numericality: {
                    onlyInteger: true,
                }
            }
        };

        let validation = validate(req.query, constraints);
        let wrongInputs = 0;

        // Find invalid values in the query
        for (let prop in validation) {
            if (!validation.hasOwnProperty(prop)) {
                continue;
            }
            data.errors[prop] = validation[prop];
            wrongInputs++;
        }

        // If there is no invalid values in the query, search for the editor
        // in the database and send the data
        if (wrongInputs === 0) {
            // Remove empty strings before searching
            // (else it would select "where city = "", name = """ etc.

            //NOTE: _ doesn't work if it's not imported and will fail silently...
            let whereQuery = _.omit(req.query, function(value, key, object) {
                return value === "";
            });
            Editor.dao.find({
                where: whereQuery
            }, function (err, editors) {
                if (err) {
                    // TODO.md: convert database error codes to human-readable errors
                    data.errors.global = err.code;
                } else {
                    data.info.confirmation = editors.length + " editors found.";

                    data.editors = editors;
                }
                console.log(data);
                res.render("partials/book/editorSearchDiv", data);

            });
        } else {
            res.render("partials/book/editorSearchDiv", data);
        }
    }
}
開發者ID:JohanBaskovec,項目名稱:libraryBackEnd,代碼行數:75,代碼來源:editorController.ts


注:本文中的validate.js類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。