当前位置: 首页>>代码示例>>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;未经允许,请勿转载。