— Judgment unanimously affirmed, with costs. Memorandum: On September 18, 1967 the New York State Department of Health directed defendant Village of Middleville to cease and abate the discharge of sewage and other wastes into the waters of the State and to submit plans for sewage treatment facilities. The village entered *834into an agreement with plaintiff, Morrell Vrooman Engineers, on May 4,1971 to provide engineering services in the design and planning of the sewage treatment facility. After performing the services and obtaining State approval of its plans, plaintiff submitted a verified claim to the village in the amount of $62,030.99. When the village failed to pay that claim, this action was commenced on September 24, 1976. By way of answer, the village asserted only a general denial. At trial each party submitted a number of exhibits which were admitted by stipulation; the underlying facts of the case were stipulated and read into the record; and the village stipulated that the amount stated in the complaint was the proper valuation for the services rendered. In addition, counsel for the respective parties made brief oral argument at which time counsel for the defendant raised for the first time the following defenses: (1) that the agreement is unenforceable because no appropriation had been made therefor nor funds authorized to be borrowed as required under former section 128-a of the Village Law (now section 5-520); (2) that the agreement is unenforceable because the Commissioner of Health was not made a party to the contract as required under former section 1263-a of the Public Health Law (now ECL 17-1901); and (3) that the parties had agreed that plaintiff’s payment was contingent upon the village receiving grant money from the Federal Government. The court entered judgment on behalf of plaintiff and defendant appeals, advancing those same arguments. Insofar as pertinent here, subdivision 2 of section 5-520 of the Village Law provides that: “2. No expenditure shall be made, nor shall any contract which in any manner involves the expenditure of money or the incurring of any pecuniary liability be entered into, unless an amount has been appropriated for the particular purpose and is available therefore or has been authorized to be borrowed pursuant to the local finance law.” Plaintiff contends that the village may not assert that it failed to comply with the statute inasmuch as such matter constitutes an affirmative defense which the village has waived by its failure to plead it. CPLR 3018 (subd |b]) provides that “[a] party shall plead all matters which if not pleaded would be likely to take the adverse party by surprise or would raise issues of fact not appearing on the face of a prior pleading such as * * * facts showing illegality either by statute or common law”. A defense based on the village’s failure to comply with the statute is an affirmative defense under the criteria of CPLR 3018 (subd ibl) and thus defendant’s failure to plead it constitutes a waiver (see Potts v Village of Haverstraw, 93 F2d 506, 509 [cert den 311 US 6601 citing McNulty v City of New York, 168 NY 117; McGovern v City of New York, 234 NY 377). We note that the trial court remarked that the parties had stipulated that the court could consider this argument by defendant, but there is nothing in the record which supports that statement. The case was submitted primarily on stipulated facts and exhibits but there does not appear to be either a written or oral stipulation that the court should consider the three defenses set forth above. Even if that defense were not deemed to be waived, however, plaintiff would be entitled to recover on a theory of implied contract. As a general rule, a claim against a municipality in quantum meruit will not lie where the original contract is void as contrary to statute or ultra vires (see New York Tel. Co. v Town of North Hempstead, 41 NY2d 691, 696; Seif v City of Long Beach, 286 NY 382, 387). Nevertheless, there are exceptions to that general rule (see, e.g., Onondaga County Water Auth. v City of Syracuse, 74 AD2d 733, app dsmd 49 NY2d 918). A plaintiff is entitled to recover from a municipality where, as here, he has entered into a contract in good faith, the municipality possesses the authority to enter into the contract, the contract is not violative of public policy and the circumstances indicate that if plaintiff is not compensated, the municipality would be unjustly enriched (see Lindlots Realty Corp. v County of *835Suffolk, 278 NY 45, 53; and see, generally, 10 McQuillin, Municipal Corporations [3d ed, revised], §§ 29.110, 29.111). The policy underlying the rule against holding municipalities liable on an implied contract theory is that of safeguarding the taxpayers’ interest against “extravagance and collusion on the part of public officials” by requiring municipalities to abide by statutory restrictions on their contractual authority (see Corning v Village of Laurel Hollow, 48 NY2d 348, 352). In this case, however, such policy would not be contravened. The village was ordered by the State to develop a sewage treatment system and the services provided by plaintiff were essential to effectuate that directive. There is no dispute as to their value and no harm to the taxpayers. To absolve the municipality from liability, particularly when it has been significantly benefited by plaintiff’s services, would encourage disregard of the statutory safeguards by municipal officials. With respect to the village’s contention that the Commissioner of Health was a necessary party to the contract pursuant to ECL 17-1901, that argument must also fail for defendant did not plead it as an affirmative defense. Even were that not the case, we agree with the trial court that the commissioner’s order to prepare plans for a sewage treatment facility and his subsequent approval of the plans prepared by plaintiff constitute sufficient compliance with the statute. With respect to the village’s third defense, there is no evidence in the record to support its argument that plaintiff’s payment was to be contingent upon the village obtaining grant money. (Appeal from judgment of Supreme Court, Herkimer County, Ringrose, J. — services rendered.) Present — Callahan, J. P., Doerr, Denman, Boomer and Schnepp, JJ.